rwstauner has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: symbol/glob hash explanation
by BrowserUk (Patriarch) on May 03, 2011 at 03:36 UTC
    Where can I find the gory details of this construct used by glob/symbol-based modules

    See Typeglobs and Filehandles in perldata.

    Is there another way to access that hash?

    If $sym contains 'fred', then ${*$sym}{hash_key} = 'something'; is equivalent to $fred{hash_key} = 'something';.

    Or strictly:

    our %fred; $fred{hash_key} = 'something';
    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?

    There is a hash (and array and scalar et al.) associated with the STDIN glob:

    c:\test>perl -E"${*STDIN}{fred} = 'bill'; say $STDIN{fred}" bill
    they just aren't normally used for anything.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      right, I understand those examples, but what about when $sym is *Symbol::GEN0 ?

      Is that hash "hidden" because it's not a simple name?

      DB<2> x $s = Symbol::gensym 0 GLOB(0x96f82b8) -> *Symbol::GEN0 DB<3> x ${*$s}{goo} = 'ber' 0 'ber' * DB<4> x \%{*$s} 0 HASH(0x96f82e8) 'goo' => 'ber' DB<5> x *$s 0 *Symbol::GEN0 DB<6> x $s 0 GLOB(0x96f82b8) -> *Symbol::GEN0 DB<7> x $Symbol::GEN0{goo} 0 undef * DB<8> x \%Symbol::GEN0 0 HASH(0x97783c0) empty hash * DB<9> x \%{Symbol::GEN0} 0 HASH(0x97783c0) empty hash * DB<10> x \%{*Symbol::GEN0} 0 HASH(0x97783c0) empty hash * DB<11> x \%{"Symbol::GEN0"} 0 HASH(0x97783c0) empty hash

      I've tried various ways of using the reference address and haven't found anything either.

        I've tried various ways of using the reference address

        All except the right one :)

        You already did x ${*$s}{goo} = 'ber' and x *$s -> *Symbol::GEN0

        So try:x ${*Symbol::GEN0}{goo}


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.