Aspect...Before(3pm)
π Aspect::Before β bestie edition π
okay so this is from the User Contributed Perl Documentation collection, it's called Aspect...Before(3pm), and it's genuinely one of the cutest little advice types out there. let's get into it π«Ά
β¨ NAME β¨
Aspect::Advice::Before β Execute code before a function is called π
β¨ SYNOPSIS β¨
use Aspect;
before {
# Trace all calls to your module
print STDERR "Called my function " . $_->sub_name . "\n";
# Shortcut calls to foo() to always be true
if ( $_->short_name eq 'foo' ) {
return $_->return_value(1);
}
# Add an extra flag to bar() but call as normal
if ( $_->short_name eq 'bar' ) {
$_->args( $_->args, 'flag' );
}
} call qr/^ MyModule::\w+ $/
likeβ¦ the way this just works? no cap. you literally write a block and it runs before every matching sub. iconic behavior fr fr πΈ
β¨ DESCRIPTION β¨
so the before advice type is the one you reach for when you want your advice code to run prior to entry into a target function β like, before the function even gets a chance to breathe. it's implemented by Aspect::Advice::Before, which is the whole reason this page exists π
and bestie, it does more than just cause side effects that run ahead of the main code. before is particularly useful for two very specific glow-ups:
- changing parameters π¦
- shortcutting calls to functions entirely, and replacing the value they would normally return with a different value π«
like, you can just⦠swap the return value out from under it. main character energy tbh.
β οΈ IMPORTANT CAVEAT, PAY ATTENTION β οΈ β thehighestpointcut (Aspect::Pointcut::Highest) is incompatible withbefore. if you build abeforeadvice with a pointcut tree that contains ahighestpointcut, you will get an exception. it's giving "do not do this", so don't do this π
and now for the part that makes the performance girlies feral: if speed matters to your program, before is particularly interesting because it's the only implementation that can take advantage of tail calls via Perl's goto function. every other advice type has to use the more costly Sub::Uplevel to keep caller() returning correctly. so likeβ¦ before is lowkey the fast one ππ₯
β¨ AUTHORS β¨
Adam Kennedy <adamk@cpan.org> π
β¨ COPYRIGHT AND LICENSE β¨
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. π«Ά
mwah π that's the whole page. go forth and run code before things, bestie.
this page shipped as part of perl v5.36.0, dated 2023-07-02 β so if you're reading this, tell the version and the date I said hi πβ¨
π¦ where this page comes from
- debian trixie: libaspect-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libaspect-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libaspect-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libaspect-perl 1.04-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms