There are occasions when it is convenient to group my work into packages that are all coded within the same perl program. I'm finding Perl's treatment of variables that I declare with 'my' a little nonintuitive, though. I'm getting warnings about a variable masking earlier declaration in same scope, and if that's the case, I'm wondering why the variable doesn't then use the originally "set" value?

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:

"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.
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....

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

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"; } #}
The above code produces the following output:

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.