Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: How do I determine if a variable contains a type glob?

by shmem (Chancellor)
on Dec 04, 2016 at 22:53 UTC ( [id://1177175]=note: print w/replies, xml ) Need Help??


in reply to How do I determine if a variable contains a type glob?

How do I determine if a variable contains a type glob

A variable never contains a type glob. It either is a typeglob by itself - in that case the the string following the sigil can be looked up in the package's symbol table - or it contains a reference to a part of a typeglob (a typeglob slot entry container). In the latter case the reference carried inside the variable is identical to the contents of a certain typeglobs slot entry inside the symbol table. A none-typeglob (not package) variable never contains or is a type glob, all it can do is sharing the typeglob's reference for a certain type. Consider:

#!/usr/bin/perl -l our $foo = "bar"; # foo is a package variable # hence *foo typglob exists print "typeglob *foo ", exists $main::{foo} ? "exists" : "doesn't exis +t"; print "typeglob *bar ", exists $main::{bar} ? "exists" : "doesn't exis +t"; print "foo typeglob: ", *foo; print "foo scalar slot ref: ",*{"main::foo"}{SCALAR}; my $bar = *foo; print "bar content: $bar"; my $baz = *{"main::foo"}{SCALAR}; print "baz content: $baz"; print $$baz; my $quux = \$foo; # reference to scalar slot of *foo print "quux is $quux";

Note how *{"main::foo"}{SCALAR} and $baz and $quux all share the same reference, despite being different variables. So, if you want to determine whether a variable contains part of a typeglob, iterate over the keys of the current packages symbol table, and for each symbol, iterate over the typeglobs reference entries, and if those are equal to the variable's content, it shares some part(s) of a typeglob's values. That's all you can get.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: How do I determine if a variable contains a type glob?
by ikegami (Patriarch) on Dec 05, 2016 at 01:23 UTC

    A variable never contains a type glob. It either is a typeglob by itself

    Not so. Scalars can contain globs.

    my $fh = *STDOUT;

    Globs can also also contain other globs. (The slot is named GLOB.) That's how subpackages are stored.

      Indeed; thank you. Haddocks' Eyes, again.

      my $f = *STDOUT; $STDOUT{foo} = 'bar'; print *{$f}{HASH}{foo},"\n"; print ${$f}{foo},"\n"; __END__ bar bar

      ...resiling to my hermitage for a revision course of perl data.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-29 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found