Net::FTP Error Handling
by grantm (Parson) on Jul 16, 2002 at 09:34 UTC
|
When a Net::FTP operation fails, you really want to get
the error details so you know what failed. It always takes
me a while to find the details in the Net::FTP docs so I
thought I'd post here:
Initial Connection: the connection is established
when you create the object. If the connect fails, new()
will return undef and the error message will be in
$@:
my $ftp = Net::FTP->new($destserv) ||
die "Connect to $destserv failed: $@";
Commands: if a command fails, the method return
value will be undef. The error message can be accessed
using the 'message()' method. If you're checking for a
particular message, you might be better off using the
'code()' method to get the three digit FTP protocol status
code which preceded the message:
$ftp->get($file) ||
die "get($file) failed: " . $ftp->code() . ": " . $ftp->message();
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
Hi,
If you are following the above code try something like :
#!/usr/bin/perl
use Net::FTP;
my $destserv="ftp.myhost.com";
my $destuser="username";
my $destpass="password";
$ftp = Net::FTP->new($destserv) or die "error connecting\n";
$ftp->login($destuser,$destpass);
my $mypath = $ftp->pwd();
$ftp->quit();
print "FTP Path: $mypath";
Keep in mind that this is untested, its late and I am seriously sleep deprived. Have you checked out the documentation? There is a link to examples you can also have a look at.
Hope this helps
Martin | [reply] [d/l] |
|
|
|
Re: Net::FTP
by ATaylor (Initiate) on May 04, 2001 at 21:22 UTC
|
One can also use the command:
$ftp->delete($file) or die "Error in delete\n";
If you would like to delete the file from FTP server.
Aaron Taylor | [reply] |
Re: Net::FTP
by mjolnir (Initiate) on Dec 29, 2002 at 00:22 UTC
|
One FAQ I get a lot is how to make Net::FTP do mget/mput.
I suggest the following:
foreach my $file (@files)
{
# Or get...
$ftp->put($file) or die "Error putting $file: ", $ftp->message;
}
| [reply] [d/l] |
|
Or, if you want to attempt to put/get all of your files - store the errors in a scalar or array and die/log them after looping through @files:
my @errors = ();
foreach my $file (@files)
{
# Or get...
$ftp->put($file) or push (@errors, ("Error putting $file: " . $ftp
+->message));
}
die (join "\n", @errors);
| [reply] [d/l] |
|
How do you define @files?
| [reply] |
Re: Net::FTP
by Anonymous Monk on Feb 27, 2003 at 18:30 UTC
|
I'm trying to do a GET on files that match a certain pattern. I don't see where the Net::FTP function support like 'mget' etc. I only see 'get'.
Any ideas on how this could be implemented?
Paul Menard
| [reply] |
|
$ftp->get($_) for grep /\.txt$/, $ftp->ls;
---
print map { my ($m)=1<<hex($_)&11?' ':'';
$m.=substr('AHJPacehklnorstu',hex($_),1) }
split //,'2fde0abe76c36c914586c';
| [reply] [d/l] [select] |
Re: Net::FTP
by brycen (Monk) on May 02, 2006 at 03:29 UTC
|
Is there any way to avoid a temporary file in the case where the perl script itself creates the data, e.g. something like:
$ftp->login("perl",'monk');
$ftp->put("fh.txt", <FH>);
print FH qq(a,b,c,d);
$ftp->quit;
And, if not, are there other perl ftp clients worth installing? | [reply] [d/l] |
|
my $pseudo_file = "line1\nline2\n";
open my $rfd, '<', \$pseudo_file or die "open1: $!";
open my $wfd, '>', 'foo' or die "open2: $!";
print $wfd $_ while <$rfd>; ## or 'print $wfd (<$rfd>);' if file
+isn't large
close $wfd;
close $rfd;
So, according to the Net::FTP doc, I'd change your code to:
open my $fh, '<', \$pseudo_file_already_generated or die "open: $!";
$ftp->login("perl",'monk') or die "blah";
$ftp->put($fh, 'fh.txt') or warn "blah";
$ftp->quit;
Update: Better english.
| [reply] [d/l] [select] |
|
Hi,
I am trying to implement this $ftp->login.
But if the login is unsuccessful, I am getting the following error message and program gets terminated.
Uncaught exception from user code:
Cannot login Login incorrect.
Is there any way to catch / handle this unsuccessful login, so that the program can continue? | [reply] [d/l] |
|