Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

net::ftp - reading a file

by Anonymous Monk
on Dec 21, 2006 at 12:28 UTC ( [id://591082]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, How can I use net::ftp simply to read a file on a remote server and process the information in that file.

I can transfer the file to my server, open it and then process it, but I am looking for a means to bypass the step of putting the file on my server and then deleting it afterwards.

#!/usr/bin/perl -w use strict; use Net::FTP; my $home="yada.com"; my $username="user"; my $password="pwd"; my $filename='file.txt'; my $newfile = "extract.txt"; my $ftp = Net::FTP->new("$home") or die "Can't connect: $@\n"; $ftp->login($username, $password) or die "Couldn't login\n"; $ftp->get($filename, $newfile) or die "Couldn't get + $filename\n";

Replies are listed 'Best First'.
Re: net::ftp - reading a file
by imp (Priest) on Dec 21, 2006 at 12:41 UTC
    Net::FTP's documentation says:
    Get REMOTE_FILE from the server and store locally. LOCAL_FILE may be a filename or a filehandle. If not specified, the file will be stored in the current directory with the same leafname as the remote file

    To skip the local file you can provide a filehandle that writes to a scalar instead, like this:

    #!/usr/bin/perl use strict; use warnings; use Net::FTP; my $home="localhost"; my $username="user"; my $password="password"; my $filename='file.txt'; my $file_data; # $newfile uses $file_data as the backend storage, and is opened read/ +write. open my $newfile, '+>', \$file_data; my $ftp = Net::FTP->new("$home") or die "Can't connect: $@\n"; $ftp->login($username, $password) or die "Couldn't login\n"; $ftp->get($filename, $newfile) or die "Couldn't get $filename\n"; print $file_data;
      much appreciated, thanks
Re: net::ftp - reading a file
by mikeB (Friar) on Dec 21, 2006 at 18:16 UTC
    You might want to take a look at IO::Ftp.
Re: net::ftp - reading a file
by ww (Archbishop) on Dec 22, 2006 at 15:48 UTC
    Unless you consider another approach (below), you should note that the contents of the file you wish to process will be on your local box - the question there is only of whether you store it as a file or in memory, as something on the order of the scalar suggested above.

    The alternate approach (which, clearly, may be way off target for your needs and capabilities) depends on what rights you have on the remote server. Can you write to the directory where the folder exists? Do you want to? Can you install and run a script on that server, to "process" the file there?

    If the above are all "yes," and you're trying to avoid saving the file to your box, you might wish to consider writing the script just mentioned and running it there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://591082]
Approved by McDarren
Front-paged by derby
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found