anadem has asked for the wisdom of the Perl Monks concerning the following question:

(this is a question about perl 5.003) i'm trying to "improve" existing code, and adding 'use strict;'

unfortunately one of the variables is set in an "include" file which is pulled by 'require'. Can I make this work? E.g.

==== main.pl ====

use strict;
my $ReqVar;
my $LocVar;
require include_file.pl;

$LocVar = $ReqVar;

<<at this point I'd like $LocVar to be foo,
but when strict and my are used $LocVar is blank
although it IS set to foo when 'my' is omitted>>

==== end of main.pl ====


==== include_file.pl ====

$ReqVar = "foo";
1;

==== end of _include_file.pl ====

thanks for hints -- I've read APP for 'strict' and 'require' and don't know if there's actually a solution. I'm constrained by this being existing code, which works when 'strict' and 'my' are not used. (It looks like 'our' in 5.6 would do this, but we're stuck at 5.003)

Alan

Replies are listed 'Best First'.
Re: scope / strict / my / require
by dave_the_m (Monsignor) on Jul 23, 2004 at 23:02 UTC
    Replace
    my $ReqVar;
    with
    use vars qw($ReqVar);
    This is the nearest equialent 5.003 has to 'our'.

    Dave.

Re: scope / strict / my / require
by davido (Cardinal) on Jul 24, 2004 at 04:40 UTC

    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

Re: scope / strict / my / require
by Fletch (Bishop) on Jul 24, 2004 at 00:37 UTC