rwstauner has asked for the wisdom of the Perl Monks concerning the following question:
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:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: symbol/glob hash explanation
by BrowserUk (Patriarch) on May 03, 2011 at 03:36 UTC | |
by rwstauner (Acolyte) on May 03, 2011 at 04:36 UTC | |
by BrowserUk (Patriarch) on May 03, 2011 at 04:43 UTC | |
by rwstauner (Acolyte) on May 03, 2011 at 14:09 UTC | |
by BrowserUk (Patriarch) on May 03, 2011 at 15:21 UTC | |
|