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__.


In reply to creating a new symbol table glob by ysth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.