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

J

jh3an

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
 
G

Gunnar Hjalmarsson

jh3an said:
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.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top