Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Net::FTP

by vaevictus (Pilgrim)
on Sep 13, 2000 at 21:25 UTC ( #32298=modulereview: print w/ replies, xml ) Need Help??

Item Description: provides simple OO interface to the File Transfer Protocol

Review Synopsis: basic and useful, a must have.

Net::FTP

Description

Net::FTP, part of the Libnet install, implements RFC959, the FTP protocol.

Who should use it?

  • Anyone wishing to transfer files to an FTP server
  • Anyone wishing to transfer data from STDIN to an FTP server
  • New Perl Scripters who need to practice with OO interfaces

What are the drawbacks or problems?

  • It does not allow you to upload scalar data as a file
  • It does not allow you to use streams other than STDIN

Example

#!/usr/bin/perl -w use Net::FTP; my $destserv="ftp.perlmonks.org"; my $destuser="root"; my $destpass="joph"; my $file="yourmom.jpg"; $ftp = Net::FTP->new($destserv) or die "error connecting\n"; $ftp->login($destuser,$destpass); $ftp->binary(); $ftp->get($file) or die "error downloading\n"; $ftp->quit();

Comment on Net::FTP
Download Code
Replies are listed 'Best First'.
Net::FTP Error Handling
by grantm (Parson) on Jul 16, 2002 at 09:34 UTC

    When a Net::FTP operation fails, you really want to get the error details so you know what failed. It always takes me a while to find the details in the Net::FTP docs so I thought I'd post here:

    Initial Connection: the connection is established when you create the object. If the connect fails, new() will return undef and the error message will be in $@:

    my $ftp = Net::FTP->new($destserv) || die "Connect to $destserv failed: $@";

    Commands: if a command fails, the method return value will be undef. The error message can be accessed using the 'message()' method. If you're checking for a particular message, you might be better off using the 'code()' method to get the three digit FTP protocol status code which preceded the message:

    $ftp->get($file) || die "get($file) failed: " . $ftp->code() . ": " . $ftp->message();
      When using this module. I cannot get the pwd command to work.

      I am connected to an ftp server and am trying to use $ftp->pwd() and I get nothing in return. Is this dumping to a default variable that I am missing or is this really a bug?

        Hi,

        If you are following the above code try something like :
        #!/usr/bin/perl use Net::FTP; my $destserv="ftp.myhost.com"; my $destuser="username"; my $destpass="password"; $ftp = Net::FTP->new($destserv) or die "error connecting\n"; $ftp->login($destuser,$destpass); my $mypath = $ftp->pwd(); $ftp->quit(); print "FTP Path: $mypath";
        Keep in mind that this is untested, its late and I am seriously sleep deprived.
        Have you checked out the documentation? There is a link to examples you can also have a look at.

        Hope this helps

        Martin
Re: Net::FTP
by ATaylor (Initiate) on May 04, 2001 at 21:22 UTC
    One can also use the command:
    $ftp->delete($file) or die "Error in delete\n";
    
    If you would like to delete the file from FTP server. Aaron Taylor
Re: Net::FTP
by mjolnir (Initiate) on Dec 29, 2002 at 00:22 UTC
    One FAQ I get a lot is how to make Net::FTP do mget/mput. I suggest the following:
    foreach my $file (@files) { # Or get... $ftp->put($file) or die "Error putting $file: ", $ftp->message; }
      Or, if you want to attempt to put/get all of your files - store the errors in a scalar or array and die/log them after looping through @files:
      my @errors = (); foreach my $file (@files) { # Or get... $ftp->put($file) or push (@errors, ("Error putting $file: " . $ftp +->message)); } die (join "\n", @errors);
        How do you define @files?
Re: Net::FTP
by Anonymous Monk on Feb 27, 2003 at 18:30 UTC
    I'm trying to do a GET on files that match a certain pattern. I don't see where the Net::FTP function support like 'mget' etc. I only see 'get'. Any ideas on how this could be implemented? Paul Menard

      You can implement mget by getting a list of files with 'ls', searching the files you want by passing the returned list through perl's 'grep' and performing a 'get' on the resulting list. Something like the following (untested):

      $ftp->get($_) for grep /\.txt$/, $ftp->ls;
      --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Net::FTP
by brycen (Monk) on May 02, 2006 at 03:29 UTC
    Is there any way to avoid a temporary file in the case where the perl script itself creates the data, e.g. something like:
    $ftp->login("perl",'monk'); $ftp->put("fh.txt", <FH>); print FH qq(a,b,c,d); $ftp->quit;
    And, if not, are there other perl ftp clients worth installing?

      DISCLAIMER: I haven't ever used Net::FTP.

      You can open scalars (v5.8.0 required) by passing a reference to that scalar to open:

      my $pseudo_file = "line1\nline2\n"; open my $rfd, '<', \$pseudo_file or die "open1: $!"; open my $wfd, '>', 'foo' or die "open2: $!"; print $wfd $_ while <$rfd>; ## or 'print $wfd (<$rfd>);' if file +isn't large close $wfd; close $rfd;

      So, according to the Net::FTP doc, I'd change your code to:

      open my $fh, '<', \$pseudo_file_already_generated or die "open: $!"; $ftp->login("perl",'monk') or die "blah"; $ftp->put($fh, 'fh.txt') or warn "blah"; $ftp->quit;

      Update: Better english.

      --
      David Serrano

Back to Reviews

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: modulereview [id://32298]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2015-07-30 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls