I want to temporarily replace a symbol table glob *foo with
a new glob (never mind why) such that \*foo has a new value.
There doesn't seem to be a way to do it. Every way to
assign to the glob just updates the GP, not actually
replacing the glob:
*foo = *bar;
*foo = Symbol::gensym
*foo = \*bar;
local *foo;
None of those work. This works, but marks all the globs
in the stash as FAKE, with disastrous consequences:
%pkg:: = (foo => *bar, %pkg::);
Any clues short of XS?
Update:
Ok, the obvious solution finally occured to me:
package pkg;
$save_old_foo = \*foo;
delete $pkg::{foo};
*{"pkg::foo"} or 0; # make perl create a new glob
...code using replaced glob goes here...
delete $pkg::{foo};
*pkg::foo = $save_old_foo;
except that there seems to be no way to restore the glob
to its original state. The above will put it at a
different memory location.
Update: judging by votes, I seem to have intrigued some people. My end use ended up somewhat pointless in the end, but I may post a meditation on it when I have some time. It involved sub {}'s undocumented interaction with *__ANON__.