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

I am making several files, one as a controller of the rest, requiring them as needed. As this is for internal use only, I am trying to make some global variables. My question is, given the following two scripts, I get a list of warnings of the type Use of uninitialized value in concatenation (.) at required.cgi line 11. My understanding was either it should work, or bomb out because of use strict, instead of merely missing the value. What I also find strange is the fact that the my ($Test2Var) = $main::TestVar; does not produce the same warning. I am using ActiveState 618. Could someone possibly explain why this is acting this way?
Thanks for your time.

Dave


control.cgi
#!perl -w use strict; $| = 1; my $TestVar = "Test"; require ("required.cgi"); my $Message = &TestPrint (0); print "Output Message : $Message\n";
required.cgi
#!perl -w use strict; sub TestPrint { #--- Variables Needed ---# my ($Received) = $_[0]; my $OutputMessage = ""; my ($Test2Var) = $main::TestVar; print "Test Var $Test2Var\n"; $OutputMessage .= "TestVar : $main::TestVar, "; $OutputMessage .= "Received : $Received"; return $OutputMessage; } 1;

Replies are listed 'Best First'.
Re: Scope of variables using strict
by japhy (Canon) on Mar 22, 2001 at 23:47 UTC
    In general, my() variables are not visible outside the scope in which they were created. That being said, required.cgi can't see the $TestVar you made in control.cgi. Nor is the $TestVar you made in control.cgi $main::TestVar. It is a lexically scoped variable, and does not belong to a package.

    To do what you're trying to do, either send $TestVar as an argument, or make it a package variable:

    # control.cgi use vars '$TestVar'; $TestVar = 'test'; # or $main::TestVar = 'test';


    japhy -- Perl and Regex Hacker
      Thanks, that was just the answer to bring everything together for me. It finally just clicked that strict is only checking for variables that were not fully qualified, rather than being defined. That, of course, makes all the difference in the world to the way variables are created.

      Dave
Re: Scope of variables using strict
by btrott (Parson) on Mar 22, 2001 at 23:45 UTC
    You have several problems: one that I can see is with your code, another is with your implementation in general.

    First, the problem with the code. You have, in control.cgi:

    my $TestVar = "Test";
    Then in required.cgi, you have:
    my ($Test2Var) = $main::TestVar;
    Your problem is that the var in control.cgi isn't in package main. my variables aren't in any package; their scope is lexical to the file or the surrouding block. Which means that your code in required.cgi has *no way* of accessing the $TestVar in control.cgi.

    So right there, you have a problem: there's no way you can get that to work if you're using lexical variables in control.cgi and trying to access them in required.cgi.

    And this comes back to your implementation: this is really not the best way to do this. If the code in required.cgi is to be used by a lot of scripts, you should package it up into either a library of exportable variables and functions or make some sort of OO interface (but *only if that makes sense*). From your message it sounds like what you may want most is the former, a library of exportable functions and variables. Take a look at perlmod and Exporter for that.