How can I read a file's contents directly with

2020-06-27 08:54发布

问题:

I want to get the file from one host to another host. We can get the file using the NET::FTP module. In that module we can use the get method to get the file. But I want the file contents instead of the file. I know that using the read method we can read the file contents. But how do I call the read function and how do I get the file contents?

回答1:

USE File::Remote for Read/write/edit remote files transparently



回答2:

From the Net::FTP documentation:

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 store the file directly into a variable attached to a filehandle.

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;


标签: perl