Need some help with Perl

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
I have the following:

Code:
$jobdir = 'P:\jobs';
# Added by PS
$batchdir = 'P:\temp';
$SEP ='\\';
$EXT = '_remove.bat';

$batfile =  $jobdir . $SEP . $customer . $SEP . $department . $SEP . $documenttype . $SEP . $job . $SEP . 'projector' . $SEP . $batch . $EXT;

#open output file
#Modified to allow batch file to be written to temp directory
#should the job directory not exist 31/08/06 - PS
unless(open(BATFILE,">$batfile")) {
	$batfile = $batchdir . $SEP . 'batch' . $EXT;
	open(BATFILE,">$batfile") || die "Cannot open $batfile";
}

Basically if the job directory doesn't exist is should place the batfile in the P:\temp directory, but nothing seems to happen however. If the job directory does exist then it doesn't enter the unless statement (as expected).
 
Paul,

I think the open statement opens the $batfile file for writing and will return false if the file cannot be opened. Also, I don't think the "open" statement actually writes anything, you need to use print to write output to the file. My Perl is a bit rusty so apologies if there are mistakes etc.

To check for a directory I think you can use the -d flag:

if (-d "mydirhere")

You could have something like:

my $batfiledir = $jobdir . ......;

if (-d "$batfiledir") {
$batfile = $jobdir . .....
} else {
$batfile = $tempdir. .....
}

open (BATFILE, >$batfile) || die "Could not open file for writing";
print BATFILE, $contentsofbat;
close BATFILE;

Let me know if that's any help.

Jim
 
Back
Top Bottom