2

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
muruga
  • 2,092
  • 2
  • 20
  • 28
  • Is it important to keep using the FTP protocol or is any protocol which solves the problem acceptable? – maerics May 13 '10 at 08:54

2 Answers2

6

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;
daxim
  • 39,270
  • 4
  • 65
  • 132
  • I gave File::Remote module is simple instead of Net::FTP – ungalnanban May 13 '10 at 09:32
  • not intending to wake up an old post, but a quick question: When you create `$remote_file_content`, and then open it and write to it, do you need to close it as well? – KingsInnerSoul Nov 13 '14 at 19:16
  • KingsInnerSoul, please [open a new question](http://stackoverflow.com/questions/ask) with that comment text, then delete your comment here. – daxim Nov 13 '14 at 21:47
-1

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

ungalnanban
  • 9,539
  • 10
  • 44
  • 55