如何使用Perl的Net :: FTP直接读取文件的内容?

mur*_*uga 2 perl

我想将文件从一个主机获取到另一个主机.我们可以使用NET :: FTP模块获取文件.在该模块中,我们可以使用该get方法来获取文件.但我想要文件内容而不是文件.我知道使用该read方法我们可以读取文件内容.但是如何调用该read函数以及如何获取文件内容?

dax*_*xim 6

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.
Run Code Online (Sandbox Code Playgroud)

因此,只需将文件直接存储到附加到文件句柄的变量中.

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;
Run Code Online (Sandbox Code Playgroud)