in reply to using $ftp->mdtm($file)

stewartski,
I was looking through the source code for Net::FTP thinking the || die piece of your code wasn't working because it returned "true but bad" response. When I couldn't find one (it returns undef if there is a problem) I noticed you are calling it in a void context - try this instead.
my $mtime = $ftp->mdtm($file); die "cannot get mdtm" if ! $mtime;
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: using $ftp->mdtm($file)
by stewartski (Initiate) on Feb 25, 2004 at 10:08 UTC
    Thanks L~R, I took your advice, but with the same result. Below is the code for my entire foreach loop. I suspect the problem might be in the first line of the loop, although the first if statement works fine.

    foreach my $file ($ftp->ls()) {
    print "in the foreach loop at $file\n";
    if (!($file=~/.?\.stm/)) { print "$file is not an .stm type file\n"; next; }
    my $mtime = $ftp->mdtm($file);
    die "cannot get mdtm\n" if ! $mtime;
    print "modified time is $mtime\n";
    chomp($mtime);
    if (!($mtime=~/\d+/)) {
    print "skipping $file\n";
    next;
    }
    if ($mtime<$timeThen) {
    print "Removing $file: ";
    $connect->delete($file);
    print $ftp->message;
    }
    }

    This works in that any file that is not a .stm file shows up in the "not an .stm file" message. The program exits on the die "cannot get mdtm" line the first time an .stm file is found.
    Any advice gratefully received
    thanks
    stewartski
      stewartski,
      Perhaps you should take a look at PerlTidy as good indention really helps in readability and bug hunting. Since you have not shown all of the code, I do not know what $timeThen is so I will assume it is an epoch time stamp. You also do not show where $connect comes from? Are you using warnings and strictures? I have never used Net::FTP, but I would guess it should look something like this.
      my $stamp; # set to appropriate epoch time stamp my @files = $ftp->ls(); chomp( @files ); for my $file ( @files ) { print "Testing file : $file\n"; if ( $file !~ /\.stm$/ ) { print "Skipping : $file is not an .stm file\n"; next; } my $mtime = $ftp->mdtm($file); die "cannot get mdtm for $file\n" if ! $mtime; print "$file last modified : ", scalar localtime($mtime), "\n"; if ( $mtime < $stamp ) { print "Removing file : $file\n"; $ftp->delete($file); print $ftp->message, "\n"; } }
      Cheers - L~R
        L~R - thanks for time & patience: I take your point about indentation. I will use <code> tags in future to preserve my code layout.

        You are almost right in your assumption that $timeThen is an epoch time stamp (in fact time-(14*24*60*60) to give time in seconds 14 days ago). Also $connect is the same as $ftp in your code. I am using warnings and strictures.

        Unfortunately, even using your modified code my program still dies on the first .stm file it finds with the 'cannot get mdtm' message.

        However, if I comment out the 'die "cannot get mdtm ..."' line, I get this error message displayed (for every .stm file in the directory): "Use of uninitialized value in localtime at mdtm_test.pl at line 36."

        (Line 36 is the 'print "$file last modified ... "' line). Then it prints the time last modified as "file name last modified : Thu Jan 1 00:00:00 1970"! The epoch if not much mistaken. Obviously my .stm files have been modified since then.

        Not entirely sure what that error message is telling me but it's a start. I realise I will have to convert either the modified time into secs or the $stamp time into an epoch time stamp (possibly using POSIX strftime).

        Thanks again for your time
        Stewartski