develooper Front page | perl.perl5.porters | Postings from February 2017

documentation for *foo{GLOB}

Dave Mitchell
February 6, 2017 17:17
documentation for *foo{GLOB}
Message ID:
perlref has this to say:

    A reference can be created by using a special syntax, lovingly known as
    the *foo{THING} syntax.  *foo{THING} returns a reference to the THING
    slot in *foo (which is the symbol table entry which holds everything
    known as foo).

        $scalarref = *foo{SCALAR};
        $arrayref  = *ARGV{ARRAY};
        $hashref   = *ENV{HASH};
        $coderef   = *handler{CODE};
        $ioref     = *STDIN{IO};
        $globref   = *foo{GLOB};
        $formatref = *foo{FORMAT};
        $globname  = *foo{NAME};    # "foo"
        $pkgname   = *foo{PACKAGE}; # "main"

    Most of these are self-explanatory, but ...

*foo{GLOB} is *not* expanded upon as one of the non-self-explanatory ones,
but I don't think it is self-explanatory. All the rest of the above access
fields within the GV or GP structs of the glob, while *foo{GLOB} returns a
reference to the glob itself. I only know this because I looked at the
source of pp_gelem() just now, then confirmed with:

    $x1 = \*foo;
    $x2 = *foo{GLOB};

    use Devel::Peek;
    Dump $x1;
    Dump $x2;

where $x1 and $x2 are shown to be references to the same glob.

I guess the docs need updating. Can anyone think of an example use for the
*foo{GLOB} syntax, when \*foo seems to do exactly the same thing but is
easier to write and understand?

The *foo{BAR} syntax appears to have been added around 5.004.

Monto Blanco... scorchio! Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About