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

Re: A general method of locally overriding subroutines

by hv (Parson)
on Mar 11, 2006 at 09:41 UTC ( #535919=note: print w/replies, xml ) Need Help??


in reply to A general method of locally overriding subroutines

Are the functions to be overridden all in the same package? If they are, you can do the localisation with a single hash slice:

sub localise_and_call { my($fn, @args) = @_; my $stash = \%::; # assuming main package # I forgot the local here @$stash{@args} = (sub { "changed" }) x @args; $fn->(@args); }

Update: oops, doesn't work when I add the "local" where it's supposed to go. I'm not sure why not though, maybe a bug.

Update: now reported as bug #38710.

Hugo

Replies are listed 'Best First'.
Re^2: A general method of locally overriding subroutines
by tmoertel (Chaplain) on Mar 11, 2006 at 16:56 UTC
    I had tried the hash-slice method earlier, but it did not work when local-ized:
    sub localize_and_call_fn { my ($locals, $fn, @args) = @_; local @::{@$locals} = (sub {"changed"}) x @$locals; $fn->(@args); }
    I figured I was doing something wrong, but I couldn't figure out where exactly the train had left the rails. Thanks for putting more light on the subject.

    Cheers,
    Tom

Re^2: A general method of locally overriding subroutines
by hv (Parson) on Mar 20, 2006 at 10:59 UTC

    Update: this bug has now been fixed for perl-5.10 (change #27547), and I think it is likely the fix will make it into one of the next maintenance releases (5.8.9 or 5.8.10) as well.

    So you will be able to write:

    sub localise_and_call { my($fn, @args) = @_; my $stash = \%::; # assuming main package local @$stash{@args} = (sub { "changed" }) x @args; $fn->(@args); }

    Hugo

Log In?
Username:
Password:

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

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












    Results (248 votes). Check out past polls.

    Notices?