Perl Monk, Perl Meditation | |
PerlMonks |
Re: Altering subroutine parameters using Hook::LexWrapby Zaxo (Archbishop) |
on Jan 19, 2005 at 04:09 UTC ( [id://423266]=note: print w/replies, xml ) | Need Help?? |
By altering $_[-1] in before(), you've instructed the pre-wrap to short-circuit and not call the wrapped sub at all. Access to wrappers is fairly easy, you just need to keep a lexical copy of the wrap call's returned object. You can kill a particular wrapper by undefining that object or letting it go out of scope. Changing @_ directly with unshift, shift, push pop or splice in a pre- routine doesn't work. It confuses Hook::LexWrap and doesn't confer aliasing to any added variables. If you want to change an argument, work on individual elements of @_. The only way I've found to change the number of arguments is to design the wrapped sub to take a reference to a data structure and work on that. Here's an example from my scratchpad of using Hook::LexWrap,
You can keep a clean copy of some_sub() around by wrapping a hard reference to it instead of a name-or-glob. Wrap then returns a reference to a newly constructed sub with wrappers, and the original is unmodified. The pre- wrapper would then be free to call the original if you liked, having short-circuited the wrapping mechanism. You may note the "not an lvalue!" comment. Hook::LexWrap does not set the original's attributes for the wrapped version. After Compline,
In Section
Seekers of Perl Wisdom
|
|