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);
In reply to Altering subroutine parameters using Hook::LexWrap by Thilosophy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |