NAME
Aspect::Point - The Join Point context
SYNOPSIS
$pointcut = call qr/^Person::[gs]et_/
& cflow company => qr/^Company::/;
# using in 'before' advice code
before {
my $context = shift; # Context is the only param to advice
print $context->type; # The advice type ('before')
print $context->pointcut; # The matching pointcut ($pointcut)
print $context->sub_name; # The full package_name::sub_name
print $context->package_name; # The package name ('Person')
print $context->short_sub_name; # The sub name (a get or set method)
print $context->self; # 1st parameter to the matching sub
print $context->params->[1]; # 2nd parameter to the matching sub
$context->append_param($rdbms); # Append a param to sub call
$context->append_params($a, $b); # Append params to sub call
$context->return_value(4) # Don't proceed and return immediately
$context->original->(x => 3); # Call matched sub independently
$context->proceed(1); # Continue to sub with context params
print $context->company->name; # Access cflow pointcut advice context
} $pointcut;
DESCRIPTION
Advice code is called when the advice pointcut is matched. In this code, there is often a need to access information about the join point context of the advice. Information like:
What is the actual sub name matched?
What are the parameters in this call that we matched?
Sometimes you want to change the context for the matched sub, such as appending a parameter or even stopping the matched sub from being called at all.
You do all these things through the Join Point
, which is an object that isa Aspect::Point. It is the only parameter provided to the advice code. It provides all the information required about the match context, and allows you to change the behavior of the matched sub.
Note: Modifying parameters through the context in the code of an after advice, will have no effect, since the matched sub has already been called.
In a future release this will be fixed so that the context for each advice type only responds to the methods relevant to that context, with the rest throwing an exception.
Cflows
If the pointcut of an advice is composed of at least one cflow
the advice code may require not only the context of the advice, but the join point context of the cflows as well.
This is required if you want to find out, for example, what the name of the sub that matched a cflow. In the synopsis example above, which method from Company
started the chain of calls that eventually reached the get/set on Person
?
You can access cflow context in the synopsis above, by calling:
$context->company;
You get it from the main advice context, by calling a method named after the context key used in the cflow spec. In the synopsis pointcut definition, the cflow part was:
cflow company => qr/^Company::/
^^^^^^^
An AdviceContext
will be created for the cflow, and you can access it using the key company
.
EXAMPLES
Print parameters to matched sub:
before {
print join ',', shift->params;
} $pointcut;
Append a parameter:
before {
shift->append_param('extra-param');
} $pointcut;
Don't proceed to matched sub, return 4 instead:
before {
shift->return_value(4);
} $pointcut;
Call matched sub again and again until it returns something defined:
after {
my $context = shift;
my $return = $context->return_value;
while ( not defined $return ) {
$return = $context->original($context->params);
}
$context->return_value($return);
} $pointcut;
Print the name of the Company
object that started the chain of calls that eventually reached the get/set on Person
:
before {
print shift->company->name;
} $pointcut;
AUTHORS
Adam Kennedy <adamk@cpan.org>
Marcel Grünauer <marcel@cpan.org>
Ran Eilam <eilara@cpan.org>
COPYRIGHT
Copyright 2001 by Marcel Grünauer
Some parts copyright 2009 - 2011 Adam Kennedy.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.