Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

by deprecated (Priest)
on Dec 13, 2001 at 07:17 UTC ( #131523=CUFP: print w/replies, xml ) Need Help??

Are you sick and tired of writing to a file with Net::FTP? Have you thought "gee, it would be nice if graham had made it possible for me to write to a scalar?"

Well, monks, I have. And tonight I decided it was time to do something about it.

So, I present Net::FTP::Scalar.

Here is the sample code, and the module is available on

#!/usr/bin/perl use warnings; use strict; use Net::FTP::Scalar; $Net::FTP::Scalar::SCALAR_MODE = 1; my $OUT_FILE; my $ftp = Net::FTP::Scalar -> new("", Debug => 0); $ftp -> login("anonymous", "foo@"); $ftp -> cwd("/in-notes"); if (not $ftp -> get("rfc1001.txt")) { $ftp -> quit; die "could not ftp ...\n"; } else { $OUT_FILE = $Net::FTP::Scalar::OUT_FILE; } print $OUT_FILE; exit 0;

Laziness, Impatience, Hubris, and Generosity.

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

    What do you think?


      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(";type=a");

      Here is

      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);


      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.


      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.


Re: Who needs files? Net::FTP::Scalar (code)
by runrig (Abbot) on Dec 13, 2001 at 12:08 UTC
    I was wondering why someone hadn't yet patched Net::FTP (or something in the family) to accept a tied filehandle. That way you could pass it an IO::Scalar filehandle and have it write to (or read from) a scalar directly. I looked at it before in a previous post and it didn't look hard to do (and princepawn had an alternative which better answered that question anyway, but could also be used here). Hmm, maybe I'll get a round tuit if no one else does...
Re: Who needs files? Net::FTP::Scalar (code)
by gbarr (Monk) on Dec 14, 2001 at 01:34 UTC
    Are you sick and tired of writing to a file with Net::FTP? Have you thought "gee, it would be nice if graham had made it possible for me to write to a scalar?"

    Um, I did. $ftp->retr($filename) will return a tied filehandle. So you can read from that in the same way you would read from a file.

      In the spirit of TMTOWTDI, though, why can't I pass Net::FTP a tied filehandle (say, an IO::Scalar), and have the data read that way?

      Update: Ok, done :)

        Because nobody has sent a patch :)
Re: Who needs files? Net::FTP::Scalar (code)
by princepawn (Parson) on Dec 13, 2001 at 14:26 UTC
    What is Net::FTP::Simple? and would you like to roll that or this into my Net::FTP::Common. Come to think of it, I've got to re-join the mailing list.
Re: Who needs files? Net::FTP::Scalar (code)
by princepawn (Parson) on Dec 13, 2001 at 14:28 UTC
    I'm the lazy type though. I would have never done all that you did to make this module.

    I would have simply used File::Temp or whatever CPAN module generates temporary filenames and then uploaded and downloaded those and then removed them.

    Maybe a little slower, maybe a bit less direct, but that is what orthogonality is all about

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://131523]
Approved by root
[karlgoethebier]: marioroy: Racket ball is considered harmful, like Squash
[marioroy]: Wait till folks see what MCE::Hobo can do in the upcoming release. Multiple instances, nested spawning, 3 levels, doesn't matter. it works on Windiows too.
[Lady_Aleena]: I hate sites that tell me to turn ad-blocker off. They should know better.
[marioroy]: s/Windiows/ Windows/
[Discipulus]: marioroy can you expand that Niagara's fall history?
erix only sees fallen angels
erix if it weren't for bad luck, I'd have no luck at all
Discipulus Windiows the OS of native americans..
[karlgoethebier]: marioroy: Next step: Write the ultimate book. "Parallel Programming With Perl. Yet Another Missing Manual" ;-)
[marioroy]: karlgoethebier I do not do Racket ball anymore. That was from gym class long ago.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-05-29 08:48 GMT
Find Nodes?
    Voting Booth?