Speedfreak has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

So I'm using NET::Ftp to transfer files on demand from a remote server. However, despite all the checks for valid characters in filenames, I cant by the nature of the beast stop users giving my script filenames to retrieve that dont exist.

What I need to do is attempt to grab a file but trap if the server gives me a "file not found" error.

I thought about just checking if the file exists after the transfer has taken place - thus proving whether or not it was downloaded but that seems rather inelegant to me.

Anyone know how to check for errors with NET::Ftp? Perldoc doesn't seem to mention anything.

The code I'm using to grab the file is thus.

	sub fetch_metar {
		$ftp = Net::FTP->new($hostname);
		$ftp->login("anonymous","metar\@speedfreak.com");		
		$ftp->get($path.$station.".TXT", $datapath.$station.".TXT");
		$ftp->quit;
	}
- Jed

Replies are listed 'Best First'.
Re: Trapping errors from NET::Ftp
by chromatic (Archbishop) on Mar 29, 2000 at 20:59 UTC
    Looking at the Net::FTP code (try it, you'll like it!) it looks like the get() method returns the name of the local file name if successful, undef if not. Try something like this:
    my $localname = $ftp->get("$path$station.TXT", "$datapath$station.TXT" +); die "Something went wrong" unless defined $localname; $ftp->quit;
    You may want more robust error handling, but I just tested that and it works.
      Net::ftp does not seem to exist on CPAN. What am I missing? sG

        I believe Net::Ftp comes with libnet. Check here for more info.

        72656B636148206C72655020726568746F6E41207473754A

        When the sun set on today, the sun is preparing tomorows beauty. here is the page from CPAN concerning NET::FTP (which im having probs with, looking through perlmonks for the 1rst time) http://theoryx5.uwinnipeg.ca/mod_perl/cpan-search?new=Search;join=and;arrange=file;download=auto;stem=no;case=clike;site=ftp.funet.fi;age=;filetype=%20module%20name;modinfo=5295
Re: Trapping errors from NET::Ftp
by lhoward (Vicar) on Mar 29, 2000 at 22:35 UTC
    Below is a little routine I wrote for checking the status of just about any of the Net:: modules (SMTP, FTP, NNTP, etc...)
    I pulled this code from memory, so it may not be quite right
    sub check_net_err{
      my $net=shift;
      if(!defined $net){
        die "Net:: object not defined $!";
      }else{
        if((!$net->ok())||($net->code() >= 400)){
          die "Net:: error ".$net->code()." : ".$net->message();
        }
      }
    }
    
    you can use it like this:
    $ftp = Net::FTP->new($hostname);
    check_net_error($ftp);
    $ftp->login("anonymous","metar\@speedfreak.com");               
    check_net_error($ftp);
    $ftp->get($path.$station.".TXT", $datapath.$station.".TXT");
    check_net_error($ftp);
    $ftp->quit;
    check_net_error($ftp);
    
    of course, you can strip down the code, and put it inline in a different format for when you want to handle errors differently (i.e. you want to die if you can't connect, but if the "get" times out you may want to retry it).
    Les Howard
    www.lesandchris.com
    Author of Net::Syslog and Number::Spell
RE: Trapping errors from NET::Ftp
by Anonymous Monk on Mar 30, 2000 at 05:16 UTC
    Simple check for the existance of the file (and possibly the size, etc) with the dir(DIR) method. $standard _ls_out = $ftp->dir($filename); then =~ for your ftp servers' error code.