I can’t seem to re-use a Net::FTP object after using quit.
Is this expected? I was unable to tell from the documentation (cpan).
As a workaround, I’m creating a new Net::FTP object each time I need to perform a batch of ftp operations. This seems wasteful.
The following example shows: successful initial login, printing of root directory ls, quit (socket close), login failing with ftp message “Connection closed”.
#!/usr/bin/env perl
use strict;
use warnings;
use Net::FTP;
my $hostname = 'foo';
my $username = 'bar';
my $password = 'baz';
# successful first pass
my $ftp = Net::FTP->new( $hostname ) or die "cannot connect to $hostname: $@";
$ftp->login( $username, $password ) or die "cannot login: ", $ftp->message;
map { print "ls_output: $_\n" } $ftp->ls; # success
$ftp->quit or die "cannot close connection: ", $ftp->message;
# re-use attempt
$ftp->login( $username, $password ) or die "cannot login: ", $ftp->message;
# never gets here since re-use attempt fails
print "done!\n";
quitcauses the remote end to close the connection and there’s no way to logout without closing the connection. If you’re trying to avoid reconnecting, you can’t.On the other hand, maybe you’re expecting
loginto connect you to the server. The connection is created innew, not inlogin, and Net::FTP does not provide a means of reconnecting.Net::FTP subclasses IO::Socket::INET, so you could reconnect by using IO::Socket::INET’s
connect, but you’d also have to reinitialise a field or two the constructor normally initialises. Nothing complicated though.But is there even a problem that needs fixing? You talk of inefficiencies, but the time it takes to create and initialise an object pales in comparison it takes to make an FTP connection.