scigeek has asked for the wisdom of the Perl Monks concerning the following question:
then strict complains about $A missing an explicit package name. That's ok, it's what strict is supposed to do. In an effort to understand and decide on the best way to fix this, I tried the following short program:package P; $A = 'something';
which generates the following output:package main; my $A = 123; $::A = 456; print ('package '.__PACKAGE__."\n"); print ('$A is '.$A."\n"); print ('our $A is '.our $A."\n"); print ('$::A is '.$::A."\n"); print ('$main::A is '.$main::A."\n"); print ('$main\'A is '.$main'A."\n");
It's the 2nd line that caught my attention. Apparently, $A (declared globally) and $::A are not the same variable. Is this correct? I seem to have missed that in the Perl documentation. And exactly what happens during the assignment (without declaration) in the first example? Does the reference to $A get converted to $P::A because of the package statement, resulting in that variable being created during the assignment?package main $A is 123 our $A is 456 $::A is 456 $main::A is 456 $main'A is 456
Discussion eqilogue - added after a couple of days
Thank you all for eventually guiding me towards the right track. The reference to the tutorial "Coping with Scoping" was especially useful. This tutorial is very precise and informative. Please bear in mind that my question was about (old) non-strict code - not best practice for the future. At least with non-strict code, the difference between a lexical alias and what used to be called a "package variable" can matter a great deal.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to declare a package variable
by AnomalousMonk (Archbishop) on Apr 15, 2016 at 02:15 UTC | |
by scigeek (Initiate) on Apr 15, 2016 at 09:34 UTC | |
by BrowserUk (Patriarch) on Apr 15, 2016 at 09:50 UTC | |
by scigeek (Initiate) on Apr 15, 2016 at 10:10 UTC | |
by BrowserUk (Patriarch) on Apr 15, 2016 at 11:09 UTC | |
by AnomalousMonk (Archbishop) on Apr 15, 2016 at 12:05 UTC | |
|
Re: how to declare a package variable
by Marshall (Canon) on Apr 15, 2016 at 00:27 UTC | |
|
Re: how to declare a package variable
by Anonymous Monk on Apr 15, 2016 at 00:32 UTC |