Click here to get back home

Why is "$A::a" and "$a" diffrent ?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Why is "$A::a" and "$a" diffrent ? jh3an 03-14-2008
Posted by jh3an on March 14, 2008, 12:38 am
Please log in for more thread options
The following code doesn't make any sense, does it ?
{
package A;
my $a = 3;
if( ! ($a == $A::a) ){print "not equal\n"}
}

#output will be:
not equal

Why ? I think $A::a equals $a.
If I do not use "my" operator, output will be the one as I expect.

Does "my" operator do something in this case?




Without my operator>>
{
package A;
$a = 3;
if(!($a == $A::a)){print "not equal\n"}
else{ print "equal\n";}
}

#output:
equal



Posted by Ignoramus17007 on March 14, 2008, 1:05 am
Please log in for more thread options
your my declaration introduced a new $a that is local in scope

> The following code doesn't make any sense, does it ?
> {
> package A;
> my $a = 3;
> if( ! ($a == $A::a) ){print "not equal\n"}
> }
>
> #output will be:
> not equal
>
> Why ? I think $A::a equals $a.
> If I do not use "my" operator, output will be the one as I expect.
>
> Does "my" operator do something in this case?
>
>
>
>
> Without my operator>>
> {
> package A;
> $a = 3;
> if(!($a == $A::a)){print "not equal\n"}
> else{ print "equal\n";}
> }
>
> #output:
> equal
>
>

Posted by Gunnar Hjalmarsson on March 14, 2008, 1:09 am
Please log in for more thread options
jh3an wrote:
> The following code doesn't make any sense, does it ?

Sure it does.

> {
> package A;
> my $a = 3;
> if( ! ($a == $A::a) ){print "not equal\n"}
> }
>
> #output will be:
> not equal
>
> Why ? I think $A::a equals $a.
> If I do not use "my" operator, output will be the one as I expect.
>
> Does "my" operator do something in this case?

It declares the lexically scoped variable $a, which is not the same
variable as the package global $A::a.

> Without my operator>>
> {
> package A;
> $a = 3;
> if(!($a == $A::a)){print "not equal\n"}
> else{ print "equal\n";}
> }
>
> #output:
> equal

In this example, $a is a package global, and $A::a is just the fully
qualified name of the very same variable.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Tad J McClellan on March 14, 2008, 7:40 am
Please log in for more thread options
> The following code doesn't make any sense, does it ?
> {
> package A;
> my $a = 3;
> if( ! ($a == $A::a) ){print "not equal\n"}
> }
>
> #output will be:
> not equal
>
> Why ? I think $A::a equals $a.


"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"


Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap