[TriLUG] Perl problem

David Rasch rasch at raschnet.com
Tue Jun 8 16:38:17 EDT 2004


On Tue, Jun 08, 2004 at 04:17:26PM -0400, Stephen Hoffman <srhoffman at bellsouth.net> wrote:
> Having a problem with a perl script.  It is mainly to flush files out of a
> directory once a nightly backup has occured.  So here's what it's SUPPOSED
> to do.
>    Look in a directory for files older then a certain number of days and
> simply delete them.  At first glance it works quite well, but upon
> further inspection I can't specify any other directory then the one
> that the perl script resides in.  So where I have $dir="." that can be
> $dir="/path/to/current/directory" and it still works, but if I specify
> a directory elsewhere it claims that the files in that directory were
> last modified on the Epoch date and are 12K+ days old.
> 
> Am I missing something?
> 
> Any help GREATLY appreciated.
> 
> Steve
> 
> 
> #!/usr/bin/perl -w
> use strict;
> 
> my $threshold = 1;
> print "Deleting files older than $threshold days\n";
> 
> my $dir = ".";
> 
> opendir(D,$dir) or die $!;
> my $time = time();
> 
> while (my $f = readdir(D)) {
>     next if $f =~ /^\./;
>     my ($atime, $mtime, $ctime) = (stat($f))[8..10];
^^ The "$f" returned by readdir needs the path prepended to it before
    statting. 

>From the 'perlfunc' manpage under 'readdir':

	       If you're planning to filetest the return values out of a
	       "readdir", you'd better prepend the directory in
	       question.  Otherwise, because we didn't "chdir" there, it
	       would have been testing the wrong file.

David



More information about the TriLUG mailing list