Typeglobs and References

K

Krishna Chaitanya

Hi everybody,

I've read in Perl books that when used as lvalues, typeglobs are
"equivalent" to references...something like this is constantly quoted
as an example of selective aliasing:

*b = \$a; # aliases $b to $a but leaves @b,%b,etc untouched

Also, I've read about the *foo{THING} notation...accessing $a as:

print ${*a{SCALAR}}; # prints the value of $a

But what is this following code I see in some places:

print ${*a}; # ALSO prints the value of $a

I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
2 are equivalent, why follow the *foo{THING} notation at all...? Seems
like the '{THING}' part is quite useless here? Am I wrong, or am I
missing anything subtle/obvious here?

Pls. enlighten.....thanks a lot.

-Chaitanya
 
C

C.DeRykus

Hi everybody,

I've read in Perl books that when used as lvalues, typeglobs are
"equivalent" to references...something like this is constantly quoted
as an example of selective aliasing:

*b = \$a; # aliases $b to $a but leaves @b,%b,etc untouched

Also, I've read about the *foo{THING} notation...accessing $a as:

print ${*a{SCALAR}}; # prints the value of $a

But what is this following code I see in some places:

print ${*a}; # ALSO prints the value of $a

I am confused.....how is ${*a} equivalent to ${*a{SCALAR}} ? If these
2 are equivalent, why follow the *foo{THING} notation at all...? Seems
like the '{THING}' part is quite useless here? Am I wrong, or am I
missing anything subtle/obvious here?

Pls. enlighten.....thanks a lot.

-Chaitanya


The sigil '$' in ${*a} provides the context that
enables the parser to lookup the glob's SCALAR
slot. In this setting, you can omit the SCALAR
but, within docs, the tag provides a quick, clear
way of identifying an individual glob slot. This
becomes very handy in perlref in the discussions
of ref/glob equivalencies for instance:

$scalarref = *foo{SCALAR};
$arrayref = *ARGV{ARRAY};
...

See: perldoc perlref

BTW, ${\$a} is also equivalent to ${*a}. You can
usually just use a reference and rarely need to
deal with the glob notation at all.
 
K

Krishna Chaitanya

Thanks a lot, Ben and Charles. It's clearer to me now...and yeah I've
gone through perlref and perlmod just now.

Ben, what I'm trying to do is to understand code someone else wrote -
which, for reasons best known to him, uses typeglobs in many
places...I want to understand this concept and replace that code with
reference-based code as much as possible (but certainly not blindly,
stupidly or without realizing why he used globs at all...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top