For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > September 2004 > Re: Net::FTP error when local file does not exist









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Net::FTP error when local file does not exist
Thomas Kratz

2004-09-28, 4:00 pm

Wild Pete wrote:
> I have a problem getting an error message using Net::FTP
> when the local file does not exist. $! seems to be empty,
> and $ftp->message() returns a message from a previous
> $ftp->cwd, not from $ftp->put. For example:
>
> $ftp->cwd("$fdir");
> $ftp->put("$file");
> $err=$!;
> $fer=$ftp->message();
> print "put $file failed $err: $fer";
>
> What I get is:
> Cannot open Local file test.txt: No such file or directory
> at test.pl line 36
> put test.txt failed : CDUP command successful.
>
> The "Cannot open" probably went to STDERR, but I can't
> seem to capture that with the above. Is this a bug?
> Suggestions?


Here the corresponding code from Net::FTP:

unless(sysopen($loc, $local, O_RDONLY))
{
carp "Cannot open Local file $local: $!\n";
return undef;
}
}

so the error message goes to STDERR via carp as you suspected. $! is reset
somewhere in between. So you can either test the existence of the local
file yourself before trying to send it (perhaps the best method), or wrap
the $ftp->put in a signal handler like this:

my $errtext;
{
local $SIG{__WARN__} = sub { $errtext = $_[0] };
$ret = $ftp->put($file);
}
print $errtext unless $ret;

Not a bug, but a questionable design perhaps. I would prefer having a
method for the last error. ($ftp->message is only for messages from the
server). IMHO, writing errors to STDERR should be up to the user of the
module. YMMV.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com