Andres12
Andres12

Reputation: 21

What is being returned by an ftp request (Perl)

my ( $addr, $usr, $pwd, $ascii, $active, $timeout ) = @_;
  my $ftp;

  # Set up new FTP with active mode and user-specified timeout...
  if ( $active and $timeout )
  {
    $ftp = Net::FTP -> new ( $addr, Passive => 0, Timeout => $timeout )
         or die "Failed to connect to FTP (w/ active, timeout): $addr";
  }


  # Login to new FTP
  $ftp -> login ( $usr, $pwd )
       or die "Failed to login to FTP: " . $ftp->message;

  # Set ASCII or binary transfer modes
  if ( $ascii ) { $ftp -> ascii();  }
  else          { $ftp -> binary(); }

  print "LOGIN: $addr\n";
  return $ftp;
}

Can someone explain what the above is doing? it's login into the ftp and then it returns it? what is it actually returning? is this for uploading or downloading?

Upvotes: 0

Views: 91

Answers (3)

David W.
David W.

Reputation: 107080

You can do a couple of things:

  1. Use Data::Dumper to dump the variable you're interested in. You will see that $ftp is a reference to a hash that contains a lot of various entries.
  2. Read up on Perl References and Perl Object Oriented Programming. You don't seem familiar with the concept of references which isn't unusual. Many beginner Perl books don't go over them and many self taught Perl experts don't know about them either.

Reading through these two tutorials will explain everything you need to know. I could try to answer your question directly, but I'd end up pretty much repeating the tutorials, and probably not do as good a job.

Use the Data::Dumper module to dump out $ftp before your return statement, and you'll get a better idea what it really looks like. Then read the tutorials.

Upvotes: 0

DVK
DVK

Reputation: 129481

It returns an object of the class Net::FTP (this way, the FTP connection is already established, and your caller code can use that object for up/downloading the files as you wish without logging in/connecting).

To learn how to use Net::FTP objects, see examples in its documentation, typically $ftp->put() and $ftp->get() to upload/download files.

Upvotes: 4

user2404501
user2404501

Reputation:

It doesn't initiate any file transfers. It just returns an object that you can use to make requests. When it returns, the FTP connection is established and the authentication is done, and the server is waiting for the next command.

Upvotes: 1

Related Questions