On 02/08/2011 14:02, Jason Forget wrote: > Hello all, > > I am receive the unintialized value in number lt (<) at error when > running the below perl script. The error is indicated on line 34. > The challenge I face is that this script was written by one person; > modified by another and I am tasked to fix it. I am a beginner to > perl scripting. From what I've read, the (<) needs to be > initialized...??? > > The script is called to delete files from a directory beyond a certain > date.
Hi Jason The Perl program that you have adopted has a lot of problems, some of which I describe below. But skip to the end of the mail to help correct the error that you describe. > #!\Perl\bin\perl -w > # usage: DeleteFiles.pl<dir> <#days> > #use strict; /Never/ disable strict. And, rather than using -w in the #! line, add use warnings; here. > # sub recurse($) { > sub recurse { That is correct. never use subroutine prototypes except in very special circumstances. > # my($path) = @_; > my($path) = $_[0]; > my($days) = $_[1]; None of these are correct. Write my ($path, $days) = @_; > my($seconds) = $days*24*60*60; That will work, but should be my $seconds = $days*24*60*60; > ## append a trailing / if it's not there > $path .= '\\' if($path !~ /\/$/); > print "Set to delete from ... $path\n"; > > ##$path = '\\Working_Temp\\'; > ##print "Hard coded to delete from: $path\n"; > ## print the directory being searched > ##print $path,"\n"; > open (MYFILE, '>>deletes.bat'); It is far better to use lexical file handles, and in any case you must check the status of an file open. So open my $myfile, '>>', 'deletes.bat' or die "Unable to append to 'deletes.bat': $!"; > ## loop through the files contained in the directory > for my $eachFile (glob($path.'*')) { > > ## if the file is a directory > if( -d $eachFile) { > ## pass the directory to the routine ( recursion ) > print "recursing $eachFile\n"; > recurse($eachFile, $days); > } else { > > ## print the file ... tabbed for readability > my $now = time; > my @stat = stat("$eachFile"); It is wrong in almost all circumstances to enclose a scalar variable in quotes. See below regarding this line. > if ($stat[9]< ($now - $seconds)) > { > #print "del $eachFile\n"; > print MYFILE "del \"$eachFile\"\n"; If you have used a lexical file handle, then this will read print $myfile "del \"$eachFile\"\n"; > unlink("$eachFile"); As before, this should be unlink $eachFile; > print "Done.\n"; > } print "\t",$eachFile,"\n"; > } > } > > } > > ## initial call ... $ARGV[0] is the first command line argument > recurse($ARGV[0], $ARGV[1]); > #close (MYFILE); There is no need to excplicitly close any input file handles, but this is an output file handle and it is possible that the file may not be written properly on closure. You should write close $myfile or die "Failed to close .bat file: $!"; As for the "unintialized value" error: Line 34 reads if ($stat[9]< ($now - $seconds)) { and your error message suggest that there is no element [9] in the @stat array. That array is assigned at line 32 my @stat = stat("$eachFile"); so there is a problem with getting the status of the file named $eachFile. As a first step, you should change that line to my @stat = stat $eachFile or die "stat failed on '$eachFile': $!"; to give you an indication about why the @stat array is empty. I hope this helps, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/