Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Typeglobs and References

Reply
Thread Tools

Typeglobs and References

 
 
Krishna Chaitanya
Guest
Posts: n/a
 
      09-15-2010
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
 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      09-15-2010
On Sep 15, 6:27*am, Krishna Chaitanya <(E-Mail Removed)> wrote:
> 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.

--
Charles DeRykus
 
Reply With Quote
 
 
 
 
Krishna Chaitanya
Guest
Posts: n/a
 
      09-16-2010
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...)
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      09-16-2010
On 2010-09-15, C.DeRykus <(E-Mail Removed)> wrote:
> BTW, ${\$a} is also equivalent to ${*a}.


Of course, it is NOT.

>perl -wle "$a=12; my $a=2; print ${\$a}; print ${*a}"

2
12

Hope this helps,
Ilya
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      09-16-2010
On Sep 16, 1:09*am, Ilya Zakharevich <(E-Mail Removed)> wrote:
> On 2010-09-15, C.DeRykus <(E-Mail Removed)> wrote:
>
> > BTW, ${\$a} is also equivalent to ${*a}.

>
> Of course, it is NOT.
>
> >perl -wle "$a=12; my $a=2; print ${\$a}; print ${*a}"

>
> 2
> 12
>


Agreed, watch your feet and no lexicals "need apply"
for this global position.


--
Charles DeRykus
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typeglobs vs References Krishna Chaitanya Perl Misc 12 03-28-2009 01:53 AM
Learning Typeglobs, Symbol Tables and the More Obscure Features? Veli-Pekka Tštilš Perl Misc 9 08-04-2006 12:22 AM
Typeglobs, Anonymous Filehandles Bernard Chan Perl Misc 3 03-07-2006 07:34 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments