Aspect::Advice(3pm)

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

πŸ’– Aspect::Advice β€” the perl doc you're about to be obsessed with πŸ’–

okay bestie, sit down, hydrate, because we're talking about Aspect::Advice(3pm), a little slice of the Perl documentation that lives in the User Contributed Perl Documentation corner of the world. she's giving AOP, she's giving pointcuts, she's giving "your code but make it ✨different✨". let's get into it πŸ¦‹


✨ NAME ✨

Aspect::Advice β€” change how Perl code is run at a pointcut. no cap, that's the whole vibe. πŸ‘‘


🌸 SYNOPSIS 🌸

okay so here's the glow-up. first, the cute sugar version where you just use Aspect and everything is easy:

# Trace calls to all functions in all MyAccount classes
use Aspect;

before {
    print 'Called: '. $_->sub_name;
} call qw/^MyAccount::/;

and then the exact same energy, but done the pure object-oriented interface way, for when you wanna be fancy and explicit about it:

# Repeat using the pure object-oriented interface
use Aspect::Advice::Before ();
use Aspect::Pointcut::Call ();

my $advice = Aspect::Advice::Before->new(
   pointcut => Aspect::Pointcut::Call->new( qr/^MyAccount::/ ),
   code     => sub {
        print 'called: '. $_->sub_name;
   },
);

two paths, same destination. main character energy either way πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so in AOP lingo, an "advice" is literally just two things glued together: a condition (which we call an Aspect::Pointcut) and some code that runs when that pointcut is true. that's it. that's the whole recipe. πŸ“

that code gets run before, after, or around the target pointcut β€” which one depends on the particular advice type declaration you used. choose your fighter.

ngl, you do not normally create advice with the constructor. the whole point is that when you use() Aspect, you get five advice declaration subroutines imported straight into your life. free gifts, no shipping. 🫢

here's the breakdown, bestie:

  • before β€” for code that should run prior to the function being called. see Aspect::Advice::Before for more info.
  • after β€” for code that should run following the function being called, and lowkey the slay part is it runs regardless of whether it returns normally or throws an exception. see Aspect::Advice::After for more info.
  • around β€” this one takes deeper control of the call and gives you your own lexical scope between the caller and callee. you must make a specific proceed call in your code to actually execute the target function. she's high maintenance but she's iconic. see Aspect::Advice::Around for more info.

when the advice code gets called, it's handed an Advice::Point object β€” that's the little context keeper that describes the context of the call to the target function and lets you change it. ✨

that parameter shows up in two places at once, bestie:

  • via the topic variable $_ (since version 0.90) πŸŽ‰
  • and as the first parameter to the advice code β€” though heads up, that second one may be deprecated at some point in the future. πŸ‘€
and if you're out here creating "advice" objects directly through the OO interface: do not use this class directly, okay?? instead use the class of the particular type of advice you actually want to create. this one is the parent energy, not the one you date. πŸ’€

πŸ’œ AUTHORS πŸ’œ

these legends made it happen:

  • Adam Kennedy <adamk@cpan.org>
  • Marcel GrΓΌnauer <marcel@cpan.org>
  • Ran Eilam <eilara@cpan.org>

Copyright 2001 by Marcel GrΓΌnauer

Some parts copyright 2009 - 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, bestie πŸ₯Ή this was perl v5.36.0, dated 2023-07-02, and it's the Aspect::Advice(3pm) manual page, signed, sealed, slayed. go forth and advise. πŸ’–βœ¨πŸ¦‹πŸŽ€

πŸ“¦ where this page comes from