.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Aspect::Point 3pm" .TH Aspect::Point 3pm "2023-07-02" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Aspect::Point \- The Join Point context .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 10 \& # An anonymous function suitable for use as advice code \& # across all advice types (as it uses no limited access methods) \& my $advice_code = sub { \& print $_\->type; # The advice type (\*(Aqbefore\*(Aq) \& print $_\->pointcut; # The matching pointcut ($pointcut) \& print $_\->enclosing; # Access cflow pointcut advice context \& print $_\->sub_name; # The full package_name::sub_name \& print $_\->package_name; # The package name (\*(AqPerson\*(Aq) \& print $_\->short_name; # The sub name (a get or set method) \& print $_\->self; # 1st parameter to the matching sub \& print ($_\->args)[1]; # 2nd parameter to the matching sub \& $_\->original\->( x => 3 ); # Call matched sub independently \& $_\->return_value(4) # Set the return value \& }; .Ve .SH "DESCRIPTION" .IX Header "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: .PP What is the actual sub name matched? .PP What are the parameters in this call that we matched? .PP 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. .PP You do all these things through the \f(CW\*(C`Join Point\*(C'\fR, 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. .PP Note: Modifying parameters through the context in the code of an \fIafter\fR advice, will have no effect, since the matched sub has already been called. .PP 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. .SS "Cflows" .IX Subsection "Cflows" If the pointcut of an advice is composed of at least one \f(CW\*(C`cflow\*(C'\fR the advice code may require not only the context of the advice, but the join point context of the cflows as well. .PP 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 \&\f(CW\*(C`Company\*(C'\fR started the chain of calls that eventually reached the get/set on \f(CW\*(C`Person\*(C'\fR? .PP You can access cflow context in the synopsis above, by calling: .PP .Vb 1 \& $point\->enclosing .Ve .PP You get it from the main advice join point by calling a method named after the context key used in the cflow spec (which is \*(L"enclosing\*(R" if a custom name was not provided, in line with AspectJ terminology). In the synopsis pointcut definition, the cflow part was equivalent to: .PP .Vb 2 \& cflow enclosing => qr/^Company::/ \& ^^^^^^^^^ .Ve .PP An Aspect::Point::Static will be created for the cflow, and you can access it using the \f(CW\*(C`enclosing\*(C'\fR method. .SH "EXAMPLES" .IX Header "EXAMPLES" Print parameters to matched sub: .PP .Vb 3 \& before { \& print join \*(Aq,\*(Aq, $_\->args; \& } $pointcut; .Ve .PP Append a parameter: .PP .Vb 3 \& before { \& $_\->args( $_\->args, \*(Aqextra parameter\*(Aq ); \& } $pointcut; .Ve .PP Don't proceed to matched sub, return 4 instead: .PP .Vb 3 \& before { \& shift\->return_value(4); \& } $pointcut; .Ve .PP Call matched sub again and again until it returns something defined: .PP .Vb 8 \& after { \& my $point = shift; \& my $return = $point\->return_value; \& while ( not defined $return ) { \& $return = $point\->original($point\->params); \& } \& $point\->return_value($return); \& } $pointcut; .Ve .PP Print the name of the \f(CW\*(C`Company\*(C'\fR object that started the chain of calls that eventually reached the get/set on \f(CW\*(C`Person\*(C'\fR: .PP .Vb 3 \& before { \& print shift\->enclosing\->self\->name; \& } $pointcut; .Ve .SH "METHODS" .IX Header "METHODS" .SS "type" .IX Subsection "type" The \f(CW\*(C`type\*(C'\fR method is a convenience provided in the situation something has a Aspect::Point method and wants to know the advice declarator it is made for. .PP Returns \f(CW"before"\fR in Aspect::Advice::Before advice, \f(CW"after"\fR in Aspect::Advice::After advice, or \f(CW"around"\fR in Aspect::Advice::Around advice. .SS "pointcut" .IX Subsection "pointcut" .Vb 1 \& my $pointcut = $_\->pointcut; .Ve .PP The \f(CW\*(C`pointcut\*(C'\fR method provides access to the original join point specification (as a tree of Aspect::Pointcut objects) that the current join point matched against. .PP Please note that the pointcut returned is the full and complete pointcut tree, due to the heavy optimisation used on the actual pointcut code when it is run there is no way at the time of advice execution to indicate which specific conditions in the pointcut tree matched and which did not. .PP Returns an object which is a sub-class of Aspect::Pointcut. .SS "original" .IX Subsection "original" .Vb 1 \& $_\->original\->( 1, 2, 3 ); .Ve .PP In a pointcut, the \f(CW\*(C`original\*(C'\fR method returns a \f(CW\*(C`CODE\*(C'\fR reference to the original function before it was hooked by the Aspect weaving process. .PP Calls made to the function are unprotected, parameters and calling context will not be replicated into the function, return params and exception will not be caught. .SS "sub_name" .IX Subsection "sub_name" .Vb 4 \& # Prints "Full::Function::name" \& before { \& print $_\->sub_name . "\en"; \& } call \*(AqFull::Function::name\*(Aq; .Ve .PP The \f(CW\*(C`sub_name\*(C'\fR method returns a string with the full resolved function name at the join point the advice code is running at. .SS "package_name" .IX Subsection "package_name" .Vb 4 \& # Prints "Just::Package" \& before { \& print $_\->package_name . "\en"; \& } call \*(AqJust::Package::name\*(Aq; .Ve .PP The \f(CW\*(C`package_name\*(C'\fR parameter is a convenience wrapper around the \f(CW\*(C`sub_name\*(C'\fR method. Where \f(CW\*(C`sub_name\*(C'\fR will return the fully resolved function name, the \&\f(CW\*(C`package_name\*(C'\fR method will return just the namespace of the package of the join point. .SS "short_name" .IX Subsection "short_name" .Vb 4 \& # Prints "name" \& before { \& print $_\->short_name . "\en"; \& } call \*(AqJust::Package::name\*(Aq; .Ve .PP The \f(CW\*(C`short_name\*(C'\fR parameter is a convenience wrapper around the \f(CW\*(C`sub_name\*(C'\fR method. Where \f(CW\*(C`sub_name\*(C'\fR will return the fully resolved function name, the \&\f(CW\*(C`short_name\*(C'\fR method will return just the name of the function. .SS "args" .IX Subsection "args" .Vb 2 \& # Add a parameter to the function call \& $_\->args( $_\->args, \*(Aqmore\*(Aq ); .Ve .PP The \f(CW\*(C`args\*(C'\fR method allows you to get or set the list of parameters to a function. It is the method equivalent of manipulating the \f(CW@_\fR array. .PP It uses a slightly unusual calling convention based on list context, but does so in a way that allows your advice code to read very naturally. .PP To summarise the situation, the three uses of the \f(CW\*(C`args\*(C'\fR method are listed below, along with their \f(CW@_\fR equivalents. .PP .Vb 2 \& # Get the parameters as a list \& my @list = $_\->args; # my $list = @_; \& \& # Get the number of parameters \& my $count = $_\->args; # my $count = @_; \& \& # Set the parameters \& $_\->args( 1, 2, 3 ); # @_ = ( 1, 2, 3 ); .Ve .PP As you can see from the above example, when \f(CW\*(C`args\*(C'\fR is called in list context it returns the list of parameters. When it is called in scalar context, it returns the number of parameters. And when it is called in void context, it sets the parameters to the passed values. .PP Although this is somewhat unconventional, it does allow the most common existing uses of the older \f(CW\*(C`params\*(C'\fR method to be changed directly to the new \f(CW\*(C`args\*(C'\fR method (such as the first example above). .PP And unlike the original, you can legally call \f(CW\*(C`args\*(C'\fR in such a way as to set the function parameters to be an empty list (which you could not do with the older \f(CW\*(C`params\*(C'\fR method). .PP .Vb 2 \& # Set the function parameters to a null list \& $_\->args(); .Ve .SS "self" .IX Subsection "self" .Vb 3 \& after { \& $_\->self\->save; \& } My::Foo::set; .Ve .PP The \f(CW\*(C`self\*(C'\fR method is a convenience provided for when you are writing advice that will be working with object-oriented Perl code. It returns the first parameter to the method (which should be object), which you can then call methods on. .PP The result is advice code that is much more natural to read, as you can see in the above example where we implement an auto-save feature on the class \&\f(CW\*(C`My::Foo\*(C'\fR, writing the contents to disk every time a value is set without error. .PP At present the \f(CW\*(C`self\*(C'\fR method is implemented fairly naively, if used outside of object-oriented code it will still return something (including \f(CW\*(C`undef\*(C'\fR in the case where there were no parameters to the join point function). .SS "wantarray" .IX Subsection "wantarray" .Vb 6 \& # Return differently depending on the calling context \& if ( $_\->wantarray ) { \& $_\->return_value(5); \& } else { \& $_\->return_value(1, 2, 3, 4, 5); \& } .Ve .PP The \f(CW\*(C`wantarray\*(C'\fR method returns the \*(L"wantarray\*(R" in perlfunc context of the call to the function for the current join point. .PP As with the core Perl \f(CW\*(C`wantarray\*(C'\fR function, returns true if the function is being called in list context, false if the function is being called in scalar context, or \f(CW\*(C`undef\*(C'\fR if the function is being called in void context. .PP \&\fBBackcompatibility Note:\fR .PP Prior to Aspect 0.98 the wantarray context of the call to the join point was available not only via the \f(CW\*(C`wantarray\*(C'\fR method, but the advice code itself was called in matching wantarray context to the function call, allowing you to use plain \f(CW\*(C`wantarray\*(C'\fR in the advice code as well. .PP As all the other information about the join point was available through methods, having this one piece of metadata available different was becoming an oddity. .PP The \f(CW\*(C`wantarray\*(C'\fR context of the join point is now \fBonly\fR available by the \&\f(CW\*(C`wantarray\*(C'\fR method. .SS "exception" .IX Subsection "exception" .Vb 3 \& unless ( $_\->exception ) { \& $_\->exception(\*(AqKaboom\*(Aq); \& } .Ve .PP The \f(CW\*(C`exception\*(C'\fR method is used to get the current die message or exception object, or to set the die message or exception object. .SS "return_value" .IX Subsection "return_value" .Vb 2 \& # Add an extra value to the returned list \& $_\->return_value( $_\->return_value, \*(Aqthing\*(Aq ); .Ve .PP The \f(CW\*(C`return_value\*(C'\fR method is used to get or set the return value for the join point function, in a similar way to the normal Perl \f(CW\*(C`return\*(C'\fR keyword. .PP As with the \f(CW\*(C`args\*(C'\fR method, the \f(CW\*(C`return_value\*(C'\fR method is sensitive to the context in which it is called. .PP When called in list context, the \f(CW\*(C`return_value\*(C'\fR method returns the join point return value as a list. If the join point is called in scalar context, this will be a single-element list containing the scalar return value. If the join point is called in void context, this will be a null list. .PP When called in scalar context, the \f(CW\*(C`return_value\*(C'\fR method returns the join point return value as a scalar. If the join point is called in list context, this will be the number of vales in the return list. If the join point is called in void context, this will be \f(CW\*(C`undef\*(C'\fR .PP When called in void context, the \f(CW\*(C`return_value\*(C'\fR method sets the return value for the join point using semantics identical to the \f(CW\*(C`return\*(C'\fR keyword. .PP Because of this change in behavior based on the context in which \f(CW\*(C`return_value\*(C'\fR is called, you should generally always set \f(CW\*(C`return_value\*(C'\fR in it's own statement to prevent accidentally calling it in non-void context. .PP .Vb 2 \& # Return null (equivalent to "return;") \& $_\->return_value; .Ve .PP In advice types that can be triggered by an exception, or need to determine whether to continue to the join point function, setting a return value via \&\f(CW\*(C`return_value\*(C'\fR is seen as implicitly indicating that any exception should be suppressed, or that we do \fBnot\fR want to continue to the join point function. .PP When you call the \f(CW\*(C`return_value\*(C'\fR method this does \s-1NOT\s0 trigger an immediate \&\f(CW\*(C`return\*(C'\fR equivalent in the advice code, the lines after \f(CW\*(C`return_value\*(C'\fR will continue to be executed as normal (to provide an opportunity for cleanup operations to be done and so on). .PP If you use \f(CW\*(C`return_value\*(C'\fR inside an if/else structure you will still need to do an explicit \f(CW\*(C`return\*(C'\fR if you wish to break out of the advice code. .PP Thus, if you wish to break out of the advice code as well as return with an alternative value, you should do the following. .PP .Vb 1 \& return $_\->return_value(\*(Aqvalue\*(Aq); .Ve .PP This usage of \f(CW\*(C`return_value\*(C'\fR appears to be contrary to the above instruction that setting the return value should always be done on a standalone line to guarentee void context. .PP However, in Perl the context of the current function is inherited by a function called with return in the manner shown above. Thus the usage of \f(CW\*(C`return_value\*(C'\fR in this way alone is guarenteed to also set the return value rather than fetch it. .SH "AUTHORS" .IX Header "AUTHORS" Adam Kennedy .PP Marcel GrĂ¼nauer .PP Ran Eilam .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright 2001 by Marcel GrĂ¼nauer .PP Some parts copyright 2009 \- 2013 Adam Kennedy. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.