Although LWP::* can do FTP as I found out. And
that is probably the recommended way to go. I had already
basically finished this by the time I figured that out
So I'm going to post it here, for comments, or whatever.
Default mode is to download in Binary, you can specify Ascii like this:
my($file) = get("ftp://user:password@ftp.somewhere.com/pub/somefile.txt;type=a");
Here is Simple.pm:
package Net::FTP::Simple;
require Exporter;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(get);
$VERSION = '0.01';
use URI;
use Net::FTP::Scalar;
$Net::FTP::Scalar::SCALAR_MODE = 1;
sub get {
my($url) = shift();
my($uri) = URI->new($url);
my($user) = $uri->user();
my($pass) = $uri->password();
my($host) = $uri->host();
my($port) = $uri->port();
my($ftp) = Net::FTP::Scalar->new($host, Debug => 0) || warn("new()
+ '$host' failed: $@") && return();
$ftp->login($user, $pass) || warn("login() '$user' failed: $@") &&
+ return();
my(@path) = grep { length() } $uri->path_segments();
my($file) = pop(@path);
my($type);
if (ref($file)) {
my(@params);
($file, @params) = @$file;
foreach (@params) {
$type = $_ if (s/^type=//);
}
}
if (defined($type) && $type eq 'a') {
$ftp->ascii();
} else {
$ftp->binary();
}
foreach (@path) {
$ftp->cwd($_) || warn("cwd() '$_' failed: $@") && return();
}
unless ($ftp->get($file)) {
$ftp->quit();
warn("get() '$file' failed: $@");
return();
}
return($Net::FTP::Scalar::OUT_FILE);
}
return(1);
Staunch
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|