Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: what is the difference between *a and *a{GLOB}?

by tilly (Archbishop)
on Feb 01, 2009 at 01:28 UTC ( #740493=note: print w/replies, xml ) Need Help??

in reply to what is the difference between *a and *a{GLOB}?

I think more explanation could be helpful.

*foo is a way at getting at the symbol table entry for foo. As perlref explains, *foo has multiple slots, they are named SCALAR, ARRAY, HASH, CODE, IO, GLOB and FORMAT. Each of those is a reference to the appropriate kind of data structure. So *foo{SCALAR} is \$foo. *foo{ARRAY} is \@array \@foo, and so on. *foo{GLOB} is \*foo.

When you use *foo and are unaware of what it is, things get very confusing, very quickly. If you print *foo you will see *main::foo. If you print ${*foo} you get whatever is in $foo. If you print @{*foo} you get at @foo, and so on. You can think of (it isn't, but you can think this way) *foo as being a weird kind of object that has been overloaded like crazy so that it can be dereferenced as several different data types.

Think you've got it? OK, can you figure out why $foo is ${*foo} is ${*foo{SCALAR}} is ${*{*foo{GLOB}}}? If you can figure that out, then you understand what can be done with a symbol table entry.

In case you understood that, let me add more detail about the symbol table. If you want to find all of the defined symbols you start by looking at the keys of %::. Keys that don't end in :: are associated with values that are symbol table entries. Any keys that end in :: are associated with child symbol tables. For instance if $foo::bar is defined then %:: will contain foo:: as a key. And %foo:: (equivalently %{$::{"foo::"}} - do you see why?) will contain bar as a key, and its value will be the symbol table entry for *foo::bar.

Be warned that %:: is an alias for %main:: is an alias for %main::main:: is an alias for %main::main::main:: and so on. It is therefore not advisable to name a module "main". Also if you wish to recursively search the symbol table for something, be sure to avoid %main::main::.

If you are still with me and can keep all of this straight, then congratulations. Relatively few Perl programmers actually understand the symbol tree in as much detail as you now do. If you got lost, then don't worry, that is to be expected. It took several tries for me to learn this to the point where I could manipulate the symbol table and get the right answer. It took longer until I convinced myself that I had an understanding of what was going on.

UPDATE: johngg pointed out that I wrote \@array where I meant \@foo. Fixed.

  • Comment on Re: what is the difference between *a and *a{GLOB}?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://740493]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-08-23 05:42 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (347 votes). Check out past polls.