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.In reply to how to declare a package variable by scigeek
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |