Answer:

The ${*$s}{key} construct is in fact part of the *foo{THING} syntax (specifically *foo{HASH}) it just looks a little different: The first pair of braces in ${}{} expects something that evaluates to a hash reference, so when given a glob it translates that to *glob{HASH}.

Symbol::gensym() (used by the IO modules to get a reference to a new glob) removes the entry from the symbol table which makes it harder to find (like the namespace::*clean family of modules do with subs).

So these three lines are equivalent (assuming $s = \*Symbol::GEN0;) except that when using gensym() you can't use the latter two to refer to the hash by name anymore:

Thanks to BrowserUk for pointing us to the answer.

If you want some more interesting reading take a look at these gists from kentnl:

Original Question:

Where can I find the gory details of this construct used by glob/symbol-based modules (IO, Net::FTP, etc):

${*$sym}{hash_key} = 'something'

(I think) I understand the end result, but I'm interested in the how/why of it.

Where can I find documentation of that syntax? I don't know what it's called so I don't know how to search for it. I haven't found what I'm looking for in perldata, perlref, or perlmod talking about typeglobs and symbol tables.

I think I would understand what's happening if I could figure out how else to access this "hash" but I can't figure out how else to "spell" the hash that's being created.

DB<1> x ${*STDIN}{goo} = "ber" 0 'ber' DB<2> x \%{*STDIN} 0 HASH(0x9730178) 'goo' => 'ber' DB<3> x \%STDIN; 0 HASH(0x9730178) 'goo' => 'ber' DB<4> use Symbol DB<5> x $s = Symbol::gensym 0 GLOB(0x99fecc8) -> *Symbol::GEN0 DB<6> x ${*$s}{pea} = 'nut' 0 'nut' DB<7> x \%{*$s} 0 HASH(0x9965e48) 'pea' => 'nut'

Is there another way to access that hash? I can't find the equivalent of the STDIN example. Where is it stored? Is it a hash that's magically not accessible by name?

Thanks!


In reply to symbol/glob hash explanation by rwstauner

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.