Fellow Monks,

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:

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);
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{ $_[-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);
Is there a way to get a reference to the wrapped subroutine (ideally including any further nested wrappers)? Or another way to this altogether?

In reply to Altering subroutine parameters using Hook::LexWrap by Thilosophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.