|more useful options|
Beginners guide to Net::FTPby spurperl (Priest)
|on Aug 14, 2002 at 10:10 UTC ( #190020=perltutorial: print w/replies, xml )||Need Help??|
PreambleI have to deal with FTP a lot... uploading and getting files, mostly from the same server & directory (where files we give to customers reside). Well, I decided to try and automate things a bit, and use Perl for this purpose.
use Net::FTPI immediately plunged into CPAN and found this nice module, that seemed to be just what I needed. However, trying to find a good tutorial on the subject (the docs of this module are quite good, but with a tutorial it is possible to get started quicker), I was dissapointed and decided to write one, on a very basic level, for other monks.
Let's go ...First, connecting and logging to the desired server. As I said earlier, most of the time I just need one server with one user name (and most of these times, the same directory), so I just store them in variables.
As simple as that... neat, huh ? Well, now that we're in, all sorts of things possible with FTP can be done. For example, getting into a directory:
The most basic and useful operations are getting and putting files to/from the server. This is done with the get() and put() methods, and w/o any special options the files must reside in the directory from which your script was called. Here is the usage:
These operations will usually do for 95% of your FTP'ing, but there are more interesting things to do, and this is where Perl comes into question (what we've done so far can be automated with shell commands). For example, finding out the modification time of a file and doing something about it:
This gets the modification time in epoch seconds format. This way, it is easy to compare with dates generated by the time function. For instance:
We can also get a list of all files (similar to the output of 'ls') from the directory.
To get a listing in the long format (of ls -l), use the dir method rather than ls.
A couple of additional tidbitsWhen you have to transfer files between different operating systems, Unix and Windows for example, you should care about Binary/ASCII transfer. ASCII should be used to transfer text files (more technically - files where lines and the breaks between them are important), which includes Perl source code, POD, etc. and all the rest is transferred with Binary. To toggle between Binary and ASCII mode use:
You should see the documentation of Net::FTP for more modes (EBCDIC, for one), but most chances are you will only need these two. One more thing... Messages from the FTP server are captured in the $f->message variable. They may be error messages, but also confirmation messages (like 'CWD command successful'). One exception though is the message from the constructor new(). If it fails, the error message is recorded in $@. So, we can also open the connection like this, for a more descriptive error message:
ConclusionI hope you enjoyed this short tour into Net::FTP and find it helpful, I certainly did ! These operations (along with the usual Perl cool way of processing things) should be enough for most of the things we have to do with FTP, but the Net::FTP module includes many more, so if your usage is really advanced, take a look at the docs.
Back to Tutorials