Aspect::Advice::Around(3pm)

Aspect::Advice::Around3pm

Execute code both before and after a function

Usage

  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+ $/;

Overview

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.

Releases

7 releases since Debian 7 (Wheezy); the text changed 4 times.

  1. 7
  2. 8–10
  3. 11
  4. 12
  5. 13

Authors

Adam Kennedy <[email protected]>

From libaspect-perl 1.04-3 in Debian 13 (Trixie). Parsed by carta from the page's man source.