Net::Async::FTP - Asynchronous FTP client
Net::Async::FTP
use IO::Async::Loop; use Net::Async::FTP; my $loop = IO::Async::Loop->new(); my $ftp = Net::Async::FTP->new(); $loop->add( $ftp ); $ftp->connect( host => "ftp.example.com", on_connected => sub { $ftp->login( user => "username", pass => "password", on_login => sub { $ftp->retr( path => "README.txt", on_data => sub { my ( $data ) = @_; print "README.txt says:\n"; print $data; $loop->loop_stop; }, ); }, on_error => sub { die shift() }, ); }, on_error => sub { die shift() }, ); $loop->loop_forever;
This object class implements an asynchronous FTP client, for use in IO::Async-based programs.
The code in this module is not particularly complete. It contains a minimal implementation of a few FTP commands, not even the full minimal set the RFC suggests all clients should support. I am releasing it anyway, because it is still useful as it stands, and could easily support extra commands being added if anyone would find it useful.
The (undocumented) do_command() method provides a generic base for the currently-implemented commands, and would be the basis for new commands.
do_command()
As they say so often in the open-source world; Patches Welcome.
This function returns a new instance of a Net::Async::FTP object. As it is a subclass of IO::Async::Stream its constructor takes any arguments for that class.
IO::Async::Stream
Connects to the FTP server. Takes the following arguments:
Hostname of the server
Optional. Service name or port number to connect to. If not supplied, will use ftp.
ftp
Optional. Socket family to use. Will default to whatever getaddrinfo() returns if not supplied.
getaddrinfo()
Continuation to call when connection is successful
$on_connected->()
Continuation to call on an error
$on_error->( $message )
Sends a USER and optionally PASS command. Takes the following arguments:
USER
PASS
Username for the USER command
Password for the PASS command if required
Continuation to invoke on successful login.
$on_login->()
Continuation to invoke on an error.
Renames a file on the remote server. Takes the following arguments
Path to file to rename
Desired new path for the file
Continuation to invoke on success.
$on_done->()
Deletes a file on the remote server. Takes the following arguments
Path to file to delete
Runs a LIST command on a path on the remote server; which requests details on the file, or contents of the directory. Takes the following arguments
LIST
Path to LIST
Continuation to invoke on success. Is passed a list of lines from the LIST result in a single string.
$on_list->( $list )
The list_parsed method may be easier to use as it parses the lines.
list_parsed
Runs a LIST command on a path on the remote server; and parse the result lines. Takes the following arguments
Continuation to invoke on success. Is passed a list of files from the LIST result, one line per element.
$on_list->( @list )
The @list array will be passed a list of HASH references, each formed like
@list
HASH
The filename
A single character; f for files, d for directories
f
d
The size in bytes
The item's last modify timestamp, as a UNIX epoch time
The access mode, as a number
Runs a NLST command on a path on the remote server; which requests a list of filenames in a directory. Takes the following arguments
NLST
Path to NLST
Continuation to invoke on success. Is passed a list of names from the NLST result in a single string.
The namelist method may be easier to use as it splits the lines.
namelist
Continuation to invoke on success. Is passed a list of names from the NLST result in a list, one name per entry
$on_name->( @names )
Retrieves a file on the remote server. Takes the following arguments
Path to file to retrieve
Continuation to invoke on success. Is passed the contents of the file as a single string.
$on_data->( $content )
Runs a STAT command on a path on the remote server; which requests details on the file, or contents of the directory. Takes the following arguments
STAT
Path to STAT
Continuation to invoke on success. Is passed a list of lines from the STAT result, one line per element.
$on_stat->( @stat )
The stat_parsed method may be easier to use as it parses the lines.
stat_parsed
Runs a STAT command on a path on the remote server; and parse the result lines. Takes the following arguments
The @stat array will be passed a list of HASH references, each formed like
@stat
If STAT is invoked on a file, then @stat will contain a single reference to represent it. If invoked on a directory, the @stat will start with a reference about the directory itself (whose name will be .), then one per item in the directory, in the order the server returned the lines.
.
Stores a file on the remote server. Takes the following arguments
Path to file to store
New contents for the file
$on_stored->()
http://tools.ieft.org/html/rfc959 - FILE TRANSFER PROTOCOL (FTP)
Paul Evans <leonerd@leonerd.org.uk>
To install Net::Async::FTP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::Async::FTP
CPAN shell
perl -MCPAN -e shell install Net::Async::FTP
For more information on module installation, please visit the detailed CPAN module installation guide.