NAME
Aspect::Modular - Base class for modular aspects
SYNOPSIS
use base 'Aspect::Modular';
use Aspect qw(advice calls returns);
sub define {
my ($self, $spec) = @_;
$self->handlers_push(
advice(calls($spec), sub { ... }),
advice(returns($spec), sub { ... })
);
$self->enable;
}
DESCRIPTION
This is the base class for modular aspects. Specific modular aspects need to override the define()
method at least.
METHODS
This class implements the following methods:
new([args])
-
This is the constructor. It creates the object, then calls
init()
to handle any arguments that were passed to the constructor. init([args])
-
This method initializes newly constructed objects. Since a modular aspect most likely, when instantiated, wants to create and enable some advice, it calls the
define()
method with the same arguments asinit()
got itself. define(spec)
-
This method should be overridden in subclasses and create and enable the advice necessary to implement the modular aspect. All advice should be pushed onto the object's handler array using
handler_push()
. See subclasses for examples. enable([packages])
disable([packages])
handlers()
-
Each aspect object stores lexical handlers created by installing advice code on join points in an array called
handlers
. For example, call and return join points install advice by wrapping the affected subroutine usingHook::LexWrap
. Those wrappers are lexically bound, so if we want to disable them and restore the subroutine to its original state, all we need to do is to let the handlers go out of scope. See theHook::LexWrap
manpage for details. Obviously, if the modular aspect goes out of scope, so do the handlers.Access to these handlers is defined via a
list
property created byClass::MethodMaker
.This accessor returns the handlers. In an array context it returns them as an array and in scalar context as a reference to the array.
handlers_push(advice)
-
Pushes a list of handlers onto the handlers array. Cf.
push()
in perlfunc. handlers_pop()
-
Pops an entry off the handlers array and returns it. Cf.
pop()
in perlfunc. handlers_shift(advice)
-
Shifts a list of handlers onto the handlers array. Cf.
shift()
in perlfunc. handlers_unshift()
-
Unshifts an entry off the handlers array and returns it. Cf.
unshift()
in perlfunc. handlers_splice()
-
Splices the handlers array. Cf.
splice()
in perlfunc. handlers_clear()
-
Clears the handlers array.
handlers_count()
-
Returns the number of elements in x.
handlers_index(indices)
-
Takes a list of indices, returns a list of the corresponding values.
handlers_set(list)
-
Takes a list, treated as pairs of index => value; each given index is set to the corresponding value. No return value.
BUGS
None known so far. If you find any bugs or oddities, please do inform the author.
AUTHOR
Marcel Grunauer, <marcel@codewerk.com>
COPYRIGHT
Copyright 2001 Marcel Grunauer. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
perl(1), Aspect::Intro(3pm), Aspect::Overview(3pm).