package Aspect::Modular; use strict; use warnings; use Aspect::Library (); our $VERSION = '1.02'; our @ISA = 'Aspect::Library'; sub new { my $class = shift; my $self = bless { @_ }, $class; # Generate the appropriate advice $self->{advice} = [ $self->get_advice( $self->args ) ]; # Warn if the aspect is supposed to be permanent, # but the advice isn't created as permanent. if ( $self->lexical ) { if ( grep { not $_->lexical } @{$self->{advice}} ) { warn("$class creates lexical advice for global aspects"); } } else { if ( grep { $_->lexical } @{$self->{advice}} ) { warn("$class creates global advice for lexical aspects"); } } return $self; } sub args { @{$_[0]->{args}}; } sub lexical { $_[0]->{lexical}; } sub get_advice { my $class = ref $_[0] || $_[0]; die("Method 'get_advice' is not implemented by class '$class'"); } ###################################################################### # Optional XS Acceleration BEGIN { local $@; eval <<'END_PERL'; use Class::XSAccessor 1.08 { replace => 1, getters => { 'lexical' => 'lexical', }, }; END_PERL } 1; __END__ =pod =head1 NAME Aspect::Modular - First generation base class for reusable aspects =head1 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'; =head1 DESCRIPTION All reusable aspect inherit from this class. Such aspects are created in user code, using the C sub exported by L. You call C with the class name of the reusable aspect (it must exist in the package C), and any parameters (pointcuts, class names, code to run, etc.) the specific aspect may require. The L aspect, for example, expects 2 pointcut specs for the wormhole source and target, while the L aspect expects a pointcut object, to select the subs to be profiled. You create a reusable aspect by subclassing this class, and providing one I