Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Who needs files? Net::FTP::Scalar (code)

by staunch (Pilgrim)
on Dec 13, 2001 at 08:11 UTC ( [id://131535]=note: print w/replies, xml ) Need Help??


in reply to Who needs files? Net::FTP::Scalar (code)

Definitely a big ++, I have always wanted this functionality.
Thank you very much. This is quite nice and solves a problem I've run into many times.

I think it would be great to have yet another interface for simple transfers that is similar to LWP::Simple.
Maybe something like:

use Net::FTP::Simple qw(get); my($file) = get("ftp://user:password@ftp.somewhere.com/pub/somefile.tx +t");

What do you think?

Staunch

Replies are listed 'Best First'.
Re: Re: Who needs files? Net::FTP::Scalar (code)
by staunch (Pilgrim) on Dec 13, 2001 at 22:36 UTC
    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

Re: Re: Who needs files? Net::FTP::Scalar (code)
by deprecated (Priest) on Dec 13, 2001 at 08:30 UTC
    It would be, like, trivially easy given the code I wrote. I'm not up to doing it right now. Why don't you give it a try, and ask me if you have any questions? I'm sure other monks here would like Net::FTP::Simple.

    dep.

    --
    Laziness, Impatience, Hubris, and Generosity.

      Sorry, I wasn't asking you to necessarily write it. Just wondering if you thought it was a good idea or not.

      My skills are definitely not up to your level, but I think I'll take a crack at it tonight utilizing your module.

      Thanks again.

      Staunch

        Well, about 45 minutes into getting URI to properly break down an ftp URL and making it work with Net::FTP::Scalar I was digging through LWP::* trying to find an example of using $uri->path_segments(). I found a good example. In fact I found exactly what I was trying to do. Then I realized: LWP::Simple supports FTP.

        I should have known..after all this is Perl. perl -e 'use LWP::Simple qw(get); print(get("ftp://ftp.slackware.com/pub/slackware/slackware-current/ChangeLog.txt"));' Staunch

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-03-29 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found