How can I read the contents of a file directly using Perl Net :: FTP?

I want to get a file from one host to another host. We can get the file using the NET :: FTP module . In this module, we can use the method get

to get the file. But I want the content of the file instead of the file. I know that using the method read

we can read the contents of the file. But how can I call the function read

and how can I get the contents of the file?

+2


a source to share


2 answers


USE File :: Remote to transparently read / write / edit remote files



-1


a source


From the documentation Net::FTP

:

get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )

Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be a filename or a filehandle.

      



So, just save the file directly in a variable attached to the file descriptor.

use Net::FTP ();

my $ftp = Net::FTP->new('ftp.kde.org', Debug => 0)
  or die "Cannot connect to some.host.name: $@";

$ftp->login('anonymous', '-anonymous@')
  or die 'Cannot login ', $ftp->message;

$ftp->cwd('/pub/kde')
  or die 'Cannot change working directory ', $ftp->message;

my ($remote_file_content, $remote_file_handle);
open($remote_file_handle, '>', \$remote_file_content);

$ftp->get('README', $remote_file_handle)
  or die "get failed ", $ftp->message;

$ftp->quit;

print $remote_file_content;

      

+6


a source







All Articles