Aspect...Around(3pm)

πŸ“¦
version 1.04 Β· 3 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’… Aspect::Advice::Around β€” the 3pm girlies know

okay bestie, welcome to Aspect::Advice::Around from the User Contributed Perl Documentation section of the manual, and no this is not a drill, this is the advice type that does the most ✨


🌸 NAME 🌸

Aspect::Advice::Around β€” run your code both before AND after a function, like a little sandwich you built yourself πŸ₯ͺ


πŸŽ€ 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+ $/;

look at her. she's doing the absolute most and we're obsessed πŸ’–


✨ DESCRIPTION ✨

The around advice type lets you execute code on either side of a function β€” like, before it and after it β€” which gives you deep and precise control over how the function is going to be called, for those moments when literally none of the other advice types are good enough. main character energy only πŸ‘‘

Using around advice is also critical if you want to lexically alter the environment the call gets made in β€” exactly like in the example up there where a global variable gets temporarily changed. the vibe is situational, and the situation is this one πŸ¦‹

real talk though: this advice type is also the most computationally expensive to run πŸ’€ so if your problem can be solved with a different advice type β€” especially before β€” you should use that one instead. save your coins, bestie.

Also please note β€” and this is the part people trip over β€” unlike the other advice types, your code inside around is required to trigger the execution of the target function yourself using the proceed method. It does not happen automatically. It's giving "you have one job" πŸ™ˆ

And if you do not proceed and also do not set either a return_value or an exception? Then the function call will return undef in scalar context, or the null list () in list context. no thoughts, head empty, nothing returned 🫢


πŸ‘‘ AUTHORS πŸ‘‘

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.


and that's the whole page, periodt πŸŽ€ this was perl v5.36.0, dated 2023-07-02, and it's literally Aspect...Around(3pm) β€” go forth and wrap your functions, bestie πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from