Thilosophy has asked for the wisdom of the Perl Monks concerning the following question:
I want to wrap some subroutines with code to inject additional parameters. Hook::LexWrap seems to be the module to do this, but I cannot figure out how.
The pre-wrapper I installed receives the parameters, but is it possible to change those parameters before the wrapped subroutine receives them? The following did not work:
An alternative idea is to short-circuit the wrapped subroutine and call it directly from the wrapper, but this obviously just results in endless recursion:use strict; use Hook::LexWrap; sub some_sub{ print "@_"; } sub before{ # inject a new arg unshift @_, 'new arg'; } wrap some_sub, pre => \&before; some_sub(1,2,3);
Is there a way to get a reference to the wrapped subroutine (ideally including any further nested wrappers)? Or another way to this altogether?use strict; use Hook::LexWrap; sub some_sub{ print "@_"; } sub before{ $_[-1] = 'short-circuit'; # remove the last arg, which was inserted by Hook::LexWrap pop; # try to call the original sub with new args, # does not work and falls into recursion some_sub ( 'new arg', @_); } wrap some_sub, pre => \&before; some_sub(1,2,3);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Altering subroutine parameters using Hook::LexWrap (ez diy)
by tye (Sage) on Jan 19, 2005 at 03:42 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by BrowserUk (Patriarch) on Jan 19, 2005 at 04:22 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by Zaxo (Archbishop) on Jan 19, 2005 at 04:09 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by Thilosophy (Curate) on Jan 19, 2005 at 04:56 UTC | |
by tye (Sage) on Jan 19, 2005 at 05:05 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by merlyn (Sage) on Jan 19, 2005 at 03:23 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by BrowserUk (Patriarch) on Jan 19, 2005 at 03:49 UTC | |
by tye (Sage) on Jan 19, 2005 at 03:51 UTC | |
|
Re: Altering subroutine parameters using Hook::LexWrap
by gaal (Parson) on Jan 19, 2005 at 11:47 UTC |