DOKK / manpages / debian 11 / libaspect-perl / Aspect::Advice::Around.3pm.en
Aspect::Advice::Around(3pm) User Contributed Perl Documentation Aspect::Advice::Around(3pm)

Aspect::Advice::Around - Execute code both before and after a function

  use Aspect;
  
  around {
      # Trace all calls to your module
      print STDERR "Called my function " . $_->sub_name . "\n";
  
      # Lexically alter a global for this function
      local $MyModule::MAXSIZE = 1000;
  
      # Continue and execute the function
      $_->run_original;
  
      # Suppress exceptions for the call
      $_->return_value(1) if $_->exception;
  
  } call qr/^ MyModule::\w+ $/;

The "around" advice type is used to execute code on either side of a function, allowing deep and precise control of how the function will be called when none of the other advice types are good enough.

Using "around" advice is also critical if you want to lexically alter the environment in which the call will be made (as in the example above where a global variable is temporarily changed).

This advice type is also the most computationally expensive to run, so if your problem can be solved with the use of a different advice type, particularly "before", you should use that instead.

Please note that unlike the other advice types, your code in "around" is required to trigger the execution of the target function yourself with the "proceed" method. If you do not "proceed" and also do not set either a "return_value" or "exception", the function call will return "undef" in scalar context or the null list "()" in list context.

Adam Kennedy <adamk@cpan.org>

Copyright 2010 - 2013 Adam Kennedy.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

2021-01-07 perl v5.32.0