Hi Jason,

I'm going to comment on your code, and hopefully point to problems in it.

On Tue, 2 Aug 2011 06:02:24 -0700 (PDT)
Jason Forget <pixe...@gmail.com> 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.
> 
> #!\Perl\bin\perl -w
> # usage: DeleteFiles.pl <dir> <#days>
> #use strict;

Why is "use strict;" commented out? Every module should have "use strict;" and
"use warnings;". See:

http://perl-begin.org/tutorials/bad-elements/

> 
> # sub recurse($) {
>  sub recurse {

If you were going to get rid of the prototype (which is not a good idea - see
the above link) please don't comment-out the original line because it just
introduces clutter.

>  #      my($path) = @_;
>   my($path) = $_[0];
>         my($days) = $_[1];

This should be written as:

my ($path, $days) = @_;

You're also missing a space after the my.
>         my($seconds) = $days*24*60*60;
>   ## append a trailing / if it's not there
>   $path .= '\\' if($path !~ /\/$/);

The comment is not helpful, and introduces more clutter and a "/" is not a "\".
You also probably want to use "/" (slashes) instead of "\\" if you want your
script to be portable to UNIXes and other non-Windows platforms. Or use
File::Spec:

http://perl-begin.org/uses/sys-admin/

>   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";

So many commented out lines so much clutter.

>         open (MYFILE, '>>deletes.bat');
> 

Well, your indentation is erratic, and furthermore, you're opening the same
global filehandles, several times for each time the function is called. See
what I say about open style on http://perl-begin.org/tutorials/bad-elements/

>   ## 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");
>                         if ($stat[9] < ($now - $seconds))
>                         {
>                                 #print "del $eachFile\n";
>                                 print MYFILE "del \"$eachFile\"\n";
>                                 unlink("$eachFile");
>                                 print "Done.\n";
>                         }      print "\t",$eachFile,"\n";
>     }
>   }
> 

OK, you should definitely use opendir instead of glob, next time because it's
safer. However, in your case you should use File::Find or a similar module
(see http://perl-begin.org/uses/sys-admin/#directory_traversal ) or maybe
File::Path's rmtree() function.

Your code is not going to remove the directories themselves.

Regards,

        Shlomi Fish.

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Original Riddles - http://www.shlomifish.org/puzzles/

Larry Wall applies a patch manually quicker than GNU patch.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to