in reply to scope / strict / my / require

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