Aspect::Modular(3pm)

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

πŸ’– 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 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