Aspect::Modular(3pm)
π Aspect::Modular β the perl doc, but make it a group chat π
okay bestie, so this is Aspect::Modular, the first generation base class for reusable aspects β¨ β like, the original recipe, the blueprint your reusable aspects get built off of. it lives in the 3pm manual section, and honestly? she's giving foundational. let's get into it π«Ά
πΈ SYNOPSIS πΈ
# Subclassing to create a reusable aspect
package Aspect::Library::ConstructorTracer;
use strict;
use base 'Aspect::Modular';
use Aspect::Advice::After ();
sub get_advice {
my $self = shift;
my $pointcut = shift;
return Aspect::Advice::After->new(
lexical => $self->lexical,
pointcut => $pointcut,
code => sub {
print 'Created object: ' . shift->return_value . "\n";
},
);
}
# Using the new aspect
package main;
use Aspect;
# Print message when constructing new Person
aspect ConstructorTracer => call 'Person::new';
like, that's the whole flow π you subclass it, you write one method, and boom β reusable aspect, no cap.
π¦ DESCRIPTION π¦
so every single reusable aspect inherits from this class. all of them. the whole girlies' club π
you make these aspects in user code, using the aspect() sub that Aspect exports. you call aspect() with the class name of the reusable aspect β and heads up, that class must exist in the package Aspect::Library β plus whatever parameters that specific aspect needs (pointcuts, class names, code to run, etc., depends on the vibe of the aspect).
take the Wormhole aspect, for example: she expects 2 pointcut specs, one for the wormhole source and one for the target π meanwhile the Profiler aspect expects a pointcut object, so it knows which subs to profile. different aspects, different love languages.
so how do you create a reusable aspect? you subclass this class, and you provide one template method: get_advice() β¨
here's the tea on get_advice(): it gets called with all the parameters that were sent when user code created the aspect, and it's expected to return an Aspect::Advice object/s β those get installed while the reusable aspect is still in scope. and if the aspect() sub gets called in void context? then the reusable aspect is installed until class reloading or interpreter shutdown. periodt π
typical things a reusable aspect might wanna do, in list form because we love structure:
- Install advice on pointcuts specified by the caller
- Push (vs. OOP pull) subs and base classes into classes specified by the caller
lowkey those two bullets are the whole personality of a reusable aspect ngl π
π AUTHORS π
- Adam Kennedy <adamk@cpan.org>
- Marcel GrΓΌnauer <marcel@cpan.org>
- Ran Eilam <eilara@cpan.org>
the founding mothers/fathers of this whole situation, we stan ππ
π COPYRIGHT π
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.
okay and for the cute sign-off: this gorgeous page came out of perl v5.36.0, dated 2023-07-02, and it's the manpage for Aspect::Modular(3pm) π§ββοΈ go forth and subclass, bestie, you're literally the main character of your own aspect library βπΈπ
π¦ 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