Hi all,

So there's this strange thing you can do:

our %h; my $x = do { local %h=(a=>'3'); \%h }; my $y = do { local %h=(b=>'5'); \%h }; dd \%h; # {} dd $x; # { a => 3 } dd $y; # { b => 5 }

While I wouldn't actually do this, I couldn't seem to find any documentation of it, and I'm wondering if it's "safe" to do, or it is some kind of dark magic that should be avoided. (Also I've only tested it on 5.18 and 5.26 so far.)

The second part of my question is that the Symbol module's gensym claims to return a reference to an "anonymous" glob. But it turns out they're not actually anonymous:

use Symbol qw/gensym/; my $foo = gensym; *$foo->{bar} = 'quz'; dd \%Symbol::GEN0; # { bar => "quz" }

So I'm wondering why Symbol::gensym isn't just implemented like this, which I got from BrowserUk's post here? It seems to me that, while the globrefs are still associated with a name, they really do seem to be more "anonymous".

sub gensym { \do{ local *ANONGLOB; *ANONGLOB } }

In the thread I linked to, tye mentions that unique names make debugging a bit easier, but I am wondering if there are any other technical downsides to the above?

Thanks,
-- Hauke D

Update 2017-09-05: So Tie::StdHandle has been doing something like this for about 18 years now: my $fh = \do { local *HANDLE };


In reply to Referencing localized variables, and typeglobs by haukex

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.