There are times that, the sole purpose of FTP is to use the file, not to persist it to disk. Although you can ftp the file, store it to disk, and then open it, use it, after use, clean it up, but that sounds a little bit ugly to me (this made Net::FTP out of the picture). So in one of the project I am working on, I wrote this piece of code for myself. It is not a full implementation of FTP, but at the right size for my purpose. The original code is one function of an OO class, that's why you see all the $self->{} stuff, but for sure you can easily modify it to non-OO style. I post it here, thinking someone else might need a similar thing. (I only FTP ascii files, you might have to modify a little bit for binaries)
sub open_remote { my $self = shift; my $continue = 1; my $ftp = new IO::Socket::INET(Proto => "tcp", PeerAddr => $self->{HOST}, PeerPort => 21, Timeout => 60, Reuse => 1); if (!$ftp) { $continue = 0; print "Failed to new control socket\n" if ($self->{DEBUG}); } if ($continue) { my $response = <$ftp>; print $response if ($self->{DEBUG}); $continue = 0 if ($response !~ /^220/); } if ($continue) { print $ftp "USER $self->{USER}\r\n"; my $response = <$ftp>; print $response if ($self->{DEBUG}); $continue = 0 if ($response !~ /^331/); } if ($continue) { print $ftp "PASS $self->{PASSWD}\r\n"; my $response = <$ftp>; print $response if ($self->{DEBUG}); $continue = 0 if ($response !~ /^230/); } if ($continue) { print $ftp "CWD $self->{DIRECTORY}\r\n"; my $response = <$ftp>; print $response if ($self->{DEBUG}); $continue = 0 if ($response !~ /^250/); } my $data_l; my $data; if ($continue) { $data_l = new IO::Socket::INET(Proto => "tcp", LocalPort => $ftp->sockport(), Listen => 1, Timeout => 60, Reuse => 1); if (!$data_l) { $continue = 0; print "Failed to new data socket\n" if ($self->{DEBUG}); } } if ($continue) { print $ftp "RETR $self->{FILE}\r\n"; $data = $data_l->accept(); close($data_l); if (!$data) { print "failed to accept data connection\n" if ($self->{DEB +UG}); $continue = 0; } else { my $response = <$ftp>; print $response if ($self->{DEBUG}); @{$self->{DATA}} = <$data>; close($data); $response = <$ftp>; print $response if ($self->{DEBUG}); } } if ($ftp) { print $ftp "QUIT\r\n"; my $response = <$ftp>; print $response if ($self->{DEBUG}); close($ftp); } }

Replies are listed 'Best First'.
Re: ftp, and store file in memory instead of persist to disk
by staunch (Pilgrim) on Feb 19, 2003 at 16:56 UTC
    You might want to check out Who needs files? Net::FTP::Scalar (code) where this topic was discussed previously.

    Since then, when I need to simply grab the contents of a file on an FTP server, I'll often use this:

    use LWP::Simple qw(get); my $data = get("ftp://ftp.slackware.com/pub/slackware/slackware-curren +t/ChangeLog.txt");


    Staunch

•Re: ftp, and store file in memory instead of persist to disk
by merlyn (Sage) on Feb 19, 2003 at 16:09 UTC
      Hm... IO::String is not a core module, I usually avoid using those modules, especially for simple stuffs like this ftp, where I don't see much maintenance would needed, and I would rather depend on my own bugs, instead of someone else's bugs.

      Also, it is much "cheap" (fast and use less memory) to use my little function than to use two full-scale modules (obviously excluding the memory used to store the file, which can not be avoided, and is my purpose).

      But, I really appreciate your comment.
        IO::String is not a core module
        But it is in the core in 5.8.

        I would rather depend on my own bugs, instead of someone else's bugs
        Then I would rather that you not post your buggy code here, especially when a much simpler solution is available. You are needlessly reinventing the wheel. It's fine if you want to do that in the privacy of your own cube, but a bit distracting for the community at large if you do it in public.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: ftp, and store file in memory instead of persist to disk
by steves (Curate) on Feb 20, 2003 at 03:42 UTC

    You know, I never realized until you suggested this that you could give Net::FTP's get and put methods a file handle instead of a file name. This realization opens up many possibilities. Thanks.