JadeNB has asked for the wisdom of the Perl Monks concerning the following question:
and convert it (programmatically, not by grovelling around in the original definition) intosub { BLAH }
Specifically, what I have in mind is taking a subroutine that modifies @_—possibly without modifying its original arguments (say, by splicing entries into the middle)—and applying a user-specified function to the resulting mutated array. (A natural thought is some wrapper likesub { BLAH YOUR CODE HERE }
but that doesn't work, since $outer is executing in a different lexical scope.)sub inject { my ( $outer, $inner ) = @_; return sub { $inner->(@_); goto &$outer; } }
I know that B::Deparse will handle the simple case of functions that don't close over external variables, and Data::Dump::Streamer will handle more cases, but I'm looking for something really robust.
but I'm not sure how to do it otherwise.sub uneval { my ( $a ) = @_; return ( in_current_scope => '$a' ); }
UPDATE: I forgot to mention that I wanted to do the unevaling with lexicals, not globals. It's easy with globals:
our @cache; my $id = 0; sub uneval { $cache[$id++] = $_[0]; return __PACKAGE__ . '::$cache[' . $id . ']'; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Modifying subroutines and un-eval-ing
by Fletch (Bishop) on Aug 06, 2009 at 17:02 UTC | |
by JadeNB (Chaplain) on Aug 06, 2009 at 17:51 UTC | |
|
Re: Modifying subroutines and un-eval-ing
by Anonymous Monk on Aug 06, 2009 at 15:21 UTC |