.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42) .\" .\" 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 "Hook::WrapSub 3pm" .TH Hook::WrapSub 3pm "2022-10-13" "perl v5.34.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" Hook::WrapSub \- wrap subs with pre\- and post\-call hooks .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Hook::WrapSub qw( wrap_subs unwrap_subs ); \& \& wrap_subs \e&before, \*(Aqsome_func\*(Aq, \*(Aqanother_func\*(Aq, \e&after; \& \& unwrap_subs \*(Aqsome_func\*(Aq; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module lets you wrap a function, providing one or both of functions that are called just before and just after, whenever the wrapped function is called. .PP There are a number of other modules that provide the same functionality as this module, some of them better. Have a look at the list in \s-1SEE ALSO,\s0 below, before you decide which to use. .SS "wrap_subs" .IX Subsection "wrap_subs" This function enables intercepting a call to any named function; handlers may be added both before and after the call to the intercepted function. .PP For example: .PP .Vb 1 \& wrap_subs \e&before, \*(Aqsome_func\*(Aq, \e&after; .Ve .PP In this case, whenever the sub named 'some_func' is called, the &before sub is called first, and the &after sub is called afterwards. These are both optional. If you only want to intercept the call beforehand: .PP .Vb 1 \& wrap_subs \e&before, \*(Aqsome_func\*(Aq; .Ve .PP You may pass more than one sub name: .PP .Vb 1 \& wrap_subs \e&before, \*(Aqfoo\*(Aq, \*(Aqbar\*(Aq, \*(Aqbaz\*(Aq, \e&after; .Ve .PP and each one will have the same hooks applied. .PP The sub names may be qualified. Any unqualified names are assumed to reside in the package of the caller. .PP The &before sub and the &after sub are both passed the argument list which is destined for the wrapped sub. This can be inspected, and even altered, in the &before sub: .PP .Vb 6 \& sub before { \& ref($_[1]) && $_[1] =~ /\ebARRAY\eb/ \& or croak "2nd arg must be an array\-ref!"; \& @_ or @_ = qw( default values ); \& # if no args passed, insert some default values \& } .Ve .PP The &after sub is also passed this list. Modifications to it will (obviously) not be seen by the wrapped sub, but the caller will see the changes, if it happens to be looking. .PP Here's an example that causes a certain method call to be redirected to a specific object. (Note, we use splice to change \f(CW$_\fR[0], because assigning directly to \f(CW$_\fR[0] would cause the change to be visible to the caller, due to the magical aliasing nature of \f(CW@_\fR.) .PP .Vb 1 \& my $handler_object = new MyClass; \& \& Hook::WrapSub::wrap_subs \& sub { splice @_, 0, 1, $handler_object }, \& \*(AqMyClass::some_method\*(Aq; \& \& my $other_object = new MyClass; \& $other_object\->some_method; \& \& # even though the method is invoked on \& # $other_object, it will actually be executed \& # with a 0\*(Aqth argument = $handler_obj, \& # as arranged by the pre\-call hook sub. .Ve .SS "Package Variables" .IX Subsection "Package Variables" There are some Hook::WrapSub package variables defined, which the &before and &after subs may inspect. .ie n .IP "$Hook::WrapSub::name" 4 .el .IP "\f(CW$Hook::WrapSub::name\fR" 4 .IX Item "$Hook::WrapSub::name" This is the fully qualified name of the wrapped sub. .ie n .IP "@Hook::WrapSub::caller" 4 .el .IP "\f(CW@Hook::WrapSub::caller\fR" 4 .IX Item "@Hook::WrapSub::caller" This is a list which strongly resembles the result of a call to the built-in function \f(CW\*(C`caller\*(C'\fR; it is provided because calling \f(CW\*(C`caller\*(C'\fR will in fact produce confusing results; if your sub is inclined to call \f(CW\*(C`caller\*(C'\fR, have it look at this variable instead. .ie n .IP "@Hook::WrapSub::result" 4 .el .IP "\f(CW@Hook::WrapSub::result\fR" 4 .IX Item "@Hook::WrapSub::result" This contains the result of the call to the wrapped sub. It is empty in the &before sub. In the &after sub, it will be empty if the sub was called in a void context, it will contain one value if the sub was called in a scalar context; otherwise, it may have any number of elements. Note that the &after function is not prevented from modifying the contents of this array; any such modifications will be seen by the caller! .PP This simple example shows how Hook::WrapSub can be used to log certain subroutine calls: .PP .Vb 6 \& sub before { \& print STDERR <<" EOF"; \& About to call $Hook::WrapSub::name( @_ ); \& Wantarray=$Hook::WrapSub::caller[5] \& EOF \& } \& \& sub after { \& print STDERR <<" EOF"; \& Called $Hook::WrapSub::name( @_ ); \& Result=( @Hook::WrapSub::result ) \& EOF \& @Hook::WrapSub::result \& or @Hook::WrapSub::result = qw( default return ); \& # if the sub failed to return something... \& } .Ve .PP Much more elaborate uses are possible. Here's one one way it could be used with database operations: .PP .Vb 1 \& my $dbh; # initialized elsewhere. \& \& wrap_subs \& sub { \& $dbh\->checkpoint \& }, \& \& \*(AqMyDb::update\*(Aq, \& \*(AqMyDb::delete\*(Aq, \& \& sub { \& # examine result of sub call: \& if ( $Hook::WrapSub::result[0] ) { \& # success \& $dbh\->commit; \& } \& else { \& # failure \& $dbh\->rollback; \& } \& }; .Ve .SS "unwrap_subs" .IX Subsection "unwrap_subs" This removes the most recent wrapping of the named subs. .PP \&\s-1NOTE:\s0 Any given sub may be wrapped an unlimited number of times. A \*(L"stack\*(R" of the wrappings is maintained internally. wrap_subs \*(L"pushes\*(R" a wrapping, and unwrap_subs \*(L"pops\*(R". .SH "SEE ALSO" .IX Header "SEE ALSO" Hook::LexWrap provides a similar capability to \f(CW\*(C`Hook::WrapSub\*(C'\fR, but has the benefit that the \f(CW\*(C`caller()\*(C'\fR function works correctly within the wrapped subroutine. .PP Sub::Prepend lets you provide a sub that will be called before a named sub. The \f(CW\*(C`caller()\*(C'\fR function works correctly in the wrapped sub. .PP Sub::Mage provides a number of related functions. You can provide pre\- and post-call hooks, you can temporarily override a function and then restore it later, and more. .PP Class::Hook lets you add pre\- and post-call hooks around any methods called by your code. It doesn't support functions. .PP Hook::Scope lets you register callbacks that will be invoked when execution leaves the scope they were registered in. .PP Hook::PrePostCall provides an \s-1OO\s0 interface for wrapping a function with pre\- and post-call hook functions. Last updated in 1997, and marked as alpha. .PP Hook::Heckle provides an \s-1OO\s0 interface for wrapping pre\- and post-call hooks around functions or methods in a package. Not updated sinc 2003, and has a 20% failed rate on \s-1CPAN\s0 Testers. .PP Moose::Manual::MethodModifiers describes Moose's mechanism for hooking a superclass's method. The \fIbefore\fR and \fIafter\fR subs are called immediately before or after the specified methods are called. The \fIaround\fR sub wraps the superclass method, and can even decide not to invoke the superclass method. .PP Class::Method::Modifiers provides a Moose\-style mechanism for a subclass to have \fIbefore\fR, \fIafter\fR, or \fIaround\fR method modifiers. .PP Class::Wrap provides the \f(CW\*(C`wrap()\*(C'\fR function, which takes a coderef and a package name. The coderef is invoked every time a method in the package is called. .PP Sub::Versive lets you stack pre\- and post-call hooks. Last updated in 2001. .SH "REPOSITORY" .IX Header "REPOSITORY" .SH "AUTHOR" .IX Header "AUTHOR" This module was written by John Porter .PP It is now being maintained by Neil Bowers. .SH "COPYRIGHT" .IX Header "COPYRIGHT" This is free software. This software may be modified and/or distributed under the same terms as Perl itself.