The facts:

In your code examples, it looks like include_file.pl is not set up as a separate package; it is just another file containing more code that comprises package main. I just mention that because it's relevant to the rest of this post.

$ReqVar, as defined in include_file.pl, is a package global that exists in package main, introduced by virtue of 'require' pulling it in from an external file. So far so good (though a little arcane). $ReqVar, because it's a package global in package main is really just a shorthand way of expressing what the variable is really named. Its real name is $main::ReqVar.

Next you use 'my' to declare a lexical variable in the main portion of your script. That lexical variable is named $ReqVar. Lexical variables are not package variables. Thus, they don't 'stand for' some other more fully qualified name. The lexical $ReqVar is a different variable from the package global named $main::ReqVar. The fact is that since the package global $main::ReqVar can be abbreviated as just $ReqVar, it is common to say that the lexical $ReqVar masks the package global $ReqVar (unless you refer to the package global by its fully qualified name, $main::$ReqVar

That's a mouthfull. But let's demonstrate it another way:

$var1 = 100; $var2 = 200; { # The brackets just created a narrower lexical scope. my $var1 = "ABCDE"; print "Inside the narrow lexical block: $var1, $var2\n"; print "The package global \$main::var still contains $main::var\n" +; } # The } bracket just closed the narrow lexical block. print "Outside the narrow lexical block: $var1, $var2\n"; __OUTPUT__ Inside the narrow lexical block: ABCDE, 200 The package global $main::var still contains 100 Outside the narrow lexical block: 100, 200

The quick-fix solution for your specific case is either to put the line, use vars qw/ReqVar/; in your main script, instead of creating a lexical named $ReqVar, or to say my $LocVar = $main::ReqVar; instead, and not even bother with creating a lexical named $ReqVar.


Dave


In reply to Re: scope / strict / my / require by davido
in thread scope / strict / my / require by anadem

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.