3rd Ed. Perl Camel book, p. 290 says that:
Variables declared with 'my' are independent of packages; they are always visible within, and only within, their enclosing scope, regardless of any package declarations.What I originally assumed was that the package statements would provide enough scope to allow me to reuse variable names within the respective package sections. And this works fine if the packages are stored in separate files and I use 'require' to access them.
But the perl code below is producing the following error messages:
To me it would seem that if I'm getting a message about a new 'my' declaration of a variable, that the new declaration would be ignored and the previous value would still be in effect, since, according to the quote above, it would seem that the 'my' variables are all being declared within the "top level" scope of the one file (the program). But then the error messages and the output (as noted below the program) show that no info is reaching the later invocations of the variable. So it seems that these later invocations neither have nor eat any cake...."my" variable $var masks earlier declaration in same scope at E:\aa\pa +cktest.pl line 16. "my" variable $var masks earlier declaration in same scope at E:\aa\pa +cktest.pl line 29. Use of uninitialized value in concatenation (.) or string at E:\aa\pac +ktest.pl line 32. Use of uninitialized value in concatenation (.) or string at E:\aa\pac +ktest.pl line 43. Use of uninitialized value in concatenation (.) or string at E:\aa\pac +ktest.pl line 22.
When I attempt to give the packages their own scope (uncomment those {} lines in the packages to make their contents their own block), the complaints about "my" masks earlier declaration go away, but only the 'aaa' block accesses the contents of the variable. (i.e. output is the same and the other error messages still appear.)
So, is there a convenient way to insure that if I reuse names of variables across my various packages packed within a single file that I won't have collisions, or must I watch out to never reuse a variable name if I join all my packages into a single file as demonstrated here? (Secondarily, I have an uneasy feeling that my setting of variable values at the start of my packages isn't going to work, i.e. be available for the various subs, the way I want it to.)
Thanks
The above code produces the following output:package aaa; #{ use strict; use warnings 'all'; my $var = 'first var'; sub do_a { print "In 'aaa' var is set to '$var'\n"; } #} package main; use strict; use warnings 'all'; my $var; aaa::do_a(); bbb::do_b(); ccc::do_c(); print "In 'main' var is set to '$var'\n"; package bbb; #{ use strict; use warnings 'all'; my $var = 'second time for var'; sub do_b { print "In 'bbb' var is set to '$var'\n"; } #} package ccc; #{ use strict; use warnings 'all'; sub do_c { print "In 'ccc' var is set to '$var'\n"; } #}
In 'aaa' var is set to 'first var' In 'bbb' var is set to '' In 'ccc' var is set to '' In 'main' var is set to ''
Updated Steve_p - added readmore tags
In reply to Scope, package, and 'my' variables by ff
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |