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

package symbol table syntax?

by John M. Dlugosz (Monsignor)
on Nov 05, 2002 at 04:57 UTC ( [id://210384]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Why does the following:
print "what? ", $main::{'VERSION'}, "\n";
indicate that the expression is a CODE(xxxxx) reference. Shouldn't it be a GLOB?

See perlmod under "Symbol Tables": "The value in each entry of the hash is what you are referring to when you use the *name typeglob notation"

Update: I'm thinking that this hash notation only works when such a symbol actually exists, as opposed to glob notation which autovivifies. Meanwhile, it can be "messed up" by assigning to it improperly.

Solution: Normally, assigning a ref to a glob will overwrite the proper slot. If the hash entry doesn't exist, it's not a glob, so assigning to it makes it that type, not a glob!

The docs imply that this hash syntax is equivilent to the * glob syntax. It's not. The glob syntax works for symbols that don't exist (yet), and the hash only shows existing entries and behaves exactly like a standard hash with respect to auto-vivification. It doesn't know it's supposed to be a glob.

Replies are listed 'Best First'.
Re: package symbol table syntax?
by Ovid (Cardinal) on Nov 05, 2002 at 05:45 UTC

    I don't get the results that you are getting, so I suspect that there is some code missing here. However, I can make an educated guess as to what might be related to this problem. Here's some sample code.

    #!/usr/local/bin/perl -w use strict; package Foo; $Foo::VERSION = '.01'; package main; $|++; print Foo->VERSION,$/; $main::VERSION = '.02'; print main->VERSION, $/; print $main::{'VERSION'},$/; print Foo->asdf;

    And here's the output:

    .01 .02 *main::VERSION Can't locate object method "asdf" via package "Foo" at ./test.pl line +12.

    You'll notice that we had no problem calling a VERSION() method in the %Foo:: namespace (and the %main:: namespace), but we couldn't call Foo->asdf(). VERSION() is a special method in the %UNIVERSAL:: namespace which all classes inherit. By calling this as a class method on the %main:: namespace, we treat %main:: as a class, thus inheriting VERSION(). I suspect that if we see the rest of your code which exhibits this strange behavior, it will somehow be related to this issue.

    See perldoc UNIVERSAL for more details.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group.

    New address of my CGI Course.

Re: package symbol table syntax?
by broquaint (Abbot) on Nov 05, 2002 at 12:43 UTC
    ... indicate that the expression is a CODE(xxxxx) reference. Shouldn't it be a GLOB?
    With both 5.6.1 and 5.8.0 I get nothing, unless VERSION is defined in some way, in which case I get a stringified version of the glob
    shell> perl -e 'print "what? [", $main::{'VERSION'}, "]\n";' what? [] shell> perl5.8.0 -e 'print "what? [", $main::{'VERSION'}, "]\n";' what? [] shell> perl -e '$VERSION=1; \ print "what? [", $main::{'VERSION'}, "]\n";' what? [*main::VERSION] shell> perl5.8.0 -e '$VERSION=1; \ print "what? [", $main::{'VERSION'}, "]\n";' what? [*main::VERSION]
    You would only get something like GLOB(0x000000) if you were stringifying a reference to a GLOB. e.g
    shell> perl -e '$VERSION=1; \ print "what? [", \$main::{'VERSION'}, "]\n";' what? [GLOB(0x806151c)]
    And to get something like CODE(0x000000) we need to access the CODE slot of the glob
    shell> perl -e 'sub VERSION {} \ print "what? [", *main::VERSION{CODE}, "]\n";' what? [CODE(0x8107f30)]

    HTH

    _________
    broquaint

Re: package symbol table syntax?
by chromatic (Archbishop) on Nov 05, 2002 at 05:46 UTC

    You'll have to show more code. Autovivification causes it to print undef on my 5.6.1 install.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://210384]
Approved by Ovid
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-23 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found