Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: A general method of locally overriding subroutines

by xdg (Monsignor)
on Mar 11, 2006 at 05:37 UTC ( #535882=note: print w/replies, xml ) Need Help??


in reply to A general method of locally overriding subroutines

What worked for me in my experiments with localizing variables in Object::LocalVars is to build a string with all the local calls and then eval it. That would avoid the recursion. You could either include the function call and arguments in the eval directly or have the eval return just a single anonymous function that does all the localization and then the function call.

As a side note, you can also use local in a list context like my, so you could try to build that eval string along these lines:

sub localize_and_call_fn { no strict 'refs'; no warnings 'redefine'; my ($locals, $fn, @args) = @_; my $new_fcn = sub { "changed" }; my $globs = join( q{,}, map { "*$_" } @$locals); my $eval_text = << "END_EVAL"; local( $globs ) = ( \$new_fcn ) x \@\$locals; \$fn->(\@args); END_EVAL eval $eval_text; }

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://535882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2020-10-25 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (249 votes). Check out past polls.

    Notices?