I am having issues getting the -e and -d file test operators to work reliably.
Listings 1 and 2 are in the same directory, on an NTFS Windows XP SP 3 system. However, Listing 2 insists that a directory exists(it doesn't), and Listing 1 gets it right. Listing 2 is part of the main program.
Also, interesting, my logger routine is refusing to create/write to a file, even when I trap open and print with or croak
statements. I suspect the two are connected. I've included that as Listing 3.
My opinion is Perl's global variables are getting unset/set somewhere and I managed to fry them(although I've tried to be very careful).
Thanks!
Listing 1:
use strict;
use warnings;
my $dir = "somedir2";
my $result= (-e $dir);
if( ! (-e $dir))
{
print "$dir doesn't exist\n";
}
else
{
print "$dir exists\n";
}
#print "$result\n";
if(! (-d $dir))
{
print "$dir isn't a dir!\n";
}
else
{
print "$dir is a dir\n";
}
Listing 2:
#Does the output directory not exist?
open_logger("logfile.txt");
logger("initializing logfile.");
logger("Checking $outputdir for existence...");
if( ! ( -e $outputdir))
{
logger("$outputdir doesn't exist...creating");
#if so, then create it
if( ! mkdir($outputdir))
{
$result = "Could not create $outputdir: $!";
logger("Could not create $outputdir: $!");
return ($success, $result);
}
logger("Created $outputdir");
}
else
{
logger("$outputdir exists...");
}
#is the directory not a directory? then die - something's off.
if( ! ( -d $outputdir))
{
$result = "Failure: output directory $outputdir not a directory!\n";
logger("$outputdir isn't a directory");
return ($success, $result);
}
Listing 3:
my $fh_logger_package;
sub open_logger
{
my $filename = shift;
open(FILE, ">$filename") or croak("Could not init logfile: $!");
$fh_logger_package = \*FILE;
print STDERR "Opened $filename\n";
if( ! (-e $filename))
{
croak("Crikey! Did not create file!");
}
}
sub logger
{
my $message = shift;
print STDERR $message . "\n";
print $fh_logger_package $message . "\n" or croak("Could not print to filehandle...");
}