Ytrew has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to save myself some typing, and create a macro that overrides a given function with a given stub, local to the current scope. I see it looking something like this:sub test_foo { my $temp; $temp = $^W; # save warning flag status $^W=0; # ignore "function is redefined" warning local *module::bar=\&stub_for_bar; $^W = $temp; # restore warnings # test code for module::foo() goes here }
I can't think of a way to do this in perl, however. If I try to move the code into a function, the local() command has the wrong scope. If there were a way to make it run in the parent's scope, (like TCL's uplevel() command), the problem would be solved, but I don't know of a way to do that. Is there some tricky caller/AUTOLOAD/goto trick I can do to accomplish this? I'd just as soon not use the -P (C-preprocessor) option if I can avoid it.sub test_foo { override_with_stub_fn('module::bar',\&stub_for_bar); # test code for module::foo goes here }
-- Ytrew
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I localize a value to the calling function's scope?
by blokhead (Monsignor) on Oct 14, 2004 at 19:24 UTC | |
|
Re: How do I localize a value to the calling function's scope?
by dragonchild (Archbishop) on Oct 14, 2004 at 19:20 UTC | |
|
Re: How do I localize a value to the calling function's scope?
by Zaxo (Archbishop) on Oct 14, 2004 at 19:56 UTC | |
by Ytrew (Pilgrim) on Oct 15, 2004 at 18:29 UTC | |
|
Re: How do I localize a value to the calling function's scope?
by cLive ;-) (Prior) on Oct 14, 2004 at 20:37 UTC |