Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Dereferencing a reference to a typeglob? [Postfix Dereference Syntax]

by kcott (Archbishop)
on Oct 29, 2016 at 11:59 UTC ( [id://1174932]=note: print w/replies, xml ) Need Help??


in reply to Dereferencing a reference to a typeglob?

"...use v5.24;..."

"Dereferencing a reference to a typeglob" can be done with Postfix Dereference Syntax (i.e. $globref->**). Some examples:

$ perl -v | head -2 | tail -1 This is perl 5, version 24, subversion 0 (v5.24.0) built for darwin-th +read-multi-2level $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E' $ perle 'use 5.024; my $x = \*STDERR; say "OUT"; say { $x->** } "ERR"' OUT ERR $ perle 'use 5.024; my $x = \*STDERR; say "OUT"; say { $x->** } "ERR"' + > /dev/null ERR $ perle 'use 5.024; my $x = \*STDERR; say "OUT"; say { $x->** } "ERR"' + 2> /dev/null OUT $

I wrote a fair amount about using this syntax in general, just yesterday: "Re^3: Syntax Question Related to "join" on a Complex Data Structure [postderef and postderef_qq]". You'll find examples of "$arrayref->@*", which may be closer (than "$globref->**") to the type of thing you're after.

— Ken

Replies are listed 'Best First'.
Re^2: Dereferencing a reference to a typeglob? [Postfix Dereference Syntax]
by stevieb (Canon) on Oct 29, 2016 at 14:53 UTC

    ++, but this doesn't solve the OP's problem of derefing a lexical, it acts the same way as a normal deref does (unless I'm missing something):

    perl -E 'my $a=25; my $x=\*a; say $x->**; say $$x;' *main::a *main::a
      > perl -E 'my $a=25; my $x=\*a; ... '

      This code doesn't do what you think it does.

      *a is always addressing the typeglob of the global symbol(s) named a , i.e. &a, $a, @a, %a the filehandle a and the format a .

      ( see here for how to use the clearer {SLOT} syntax.)

      Maybe this code example makes it clearer:

      DB<104> $a=42; my $a=666; my $ra=\*a; print $$$ra 42

      update

      Best think of the STASH (= Symbol Table Hash) as a Hash of Hashes, where the second level "hash" is a special structure called "glob" which always has exactly 6 fixed entries, which are either undef or a reference.

      Lexicals use a totally different mechanism in the so called "Pad" and can't be addressed with * because there is nothing like a "lexical glob" unifying all lexical symbols.

      If you use PadWalker to introspect pads you'll see that $x', '%x', '@x listed as separate top level symbols.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174932]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-18 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found