Velocity Reviews > Perl > Why is "\$A::a" and "\$a" diffrent ?

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

jh3an
Guest
Posts: n/a

 03-14-2008
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

Guest
Posts: n/a

 03-14-2008
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.

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

Guest
Posts: n/a

 03-14-2008
jh3an <(E-Mail Removed)> wrote:
> 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

--
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"