logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Authors

       Adam Kennedy <adamk@cpan.org>

Description

       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.

Name

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

Synopsis

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

See Also