On 9/12/06, JupiterHost.Net <[EMAIL PROTECTED]> wrote:
I'm trying to change the behavior of a function (whose use of and call I have no control over) to avoid some nasty behavior in certain circumstances.
Its easy enough to redefine it but after the hairy part is done I want to change it back. Similar to how you can: local $var;
What you need is just a "local *What::ever = \&new_sub;" I tried to write similar to what you need. The code below overrides File::Copy::copy so that it dumps the arguments before copying. This is done inside a block: #!/usr/bin/perl use strict; use warnings; use File::Copy (); my ($source, $target) = @ARGV; { my $copy = \&File::Copy::copy; # save it #no warnings qw(redefine); local *File::Copy::copy = sub { warn "copy(@_)\n"; $copy->(@_); }; File::Copy::copy($source, $target); } # File::Copy::copy is magically restored here If you don't use the "no warnings" bit you will see a "Subroutine File::Copy::copy redefined". You don't need to save it like I did at "my $copy = \&File::Copy::copy; # save it" if you don't need the old code. In the example, I just added behavior and invoked the old code to do the hard work. What the statement does local *What::ever = \&my_sub; # with a named sub or local *What::ever = sub { }; # with a closure is to locally redefine the *What::ever{CODE} slot of the glob. Regards, Adriano. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>