Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scoping problem?

Status
Not open for further replies.

drkestrel

MIS
Sep 25, 2000
439
GB
I have a file of the following structure
In myParser.pl:
Code:
use File::Spec;
require 'mFileParser.pl';

$tempDir     = File::Spec->catfile("root","temp");  
#Some other codes
processMFile();  #defined in mFileParser.pl
#Some other codes


In mFileParser.pl
Code:
use File::Spec;
sub processMFile()
{
    #someCode

    print "DEBUG TEMPDIR-" . $tempDir ."\n";
    $tmpFile = File::Spec->catfile($tempDir,"tmp.txt");
    #someCode
}
The problem is in processMFile within the mFileParser.pl, when it is invoked via myParser.pl, the variable at
Code:
$tempDir
within
Code:
print "DEBUG TEMPDIR-" . $tempDir ."\n"
is not set to "root";

What have I done wrong? I would rather not have to initialise common/global variable across two scripts anywhere and everywhere :) ! Where should it really be initialised?

Note, there is no subroutine defined in the parent- myParser.pl
 
try putting the variable declaration above the require line.
if this doesn't work, you could just add a line at the beginning of the subroutine to take an argument for $tempdir to be set within.
another option is to change to call to $tempdir to a call to $main::tempdir, which might work as well.

HTH "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top