MadraghRua has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks
I have a pretty specific question about standard out handling with Net::FTP.

I want to run a script in a verbose mode to collect lots of information at each step of an ftp session. For each step in the FTP process I want to print a message to standard output. A truncated example is included below.

When I try to do this, the script executes with no errors but I don't know what is actually happening during each step of the ftp process - the standard output does not appear to be there for you to see or collect.

Does Net::FTP open up a seperate shell and run the ftp session through this? This might explain why I cannot capture the standard output from the ftp session from each step of the subroutine.

Should we be using Net::Cmd to capture these responses? Looking at the documentation there is mention of a series of six subroutines: CMD_INFO, CMD_OK, CMD_MORE, CMD_REJECT and CMD_ERROR ,correspond to possible results of response and status. The sixth is CMD_PENDING. However I don't see an example of how these might be used. For chuckles and grins I have read through the associated perlmonks messsages, CPAN documentation and man pages on Net::FTP and Net::Cmd and the Perl Cookbook

Finally I did see the Debug flag in the libnet documentation and we are trying this out. But again, does this faithfully pipe back the messages from the ftp server or is it simply the Net::FTP module's interpretation of the server response?

Here is a copy of the contents of a subroutine that we are using here. We have used strict and the -w flag in another portion of our script, so the code does pass with these tests. We do realize that we could have used 'or die' but we planned to do something that would be a wee bit more extensive and suitable to our needs

...omitted for clarity... print "Connecting to $url ... " ; if ($ftp = Net::FTP->new($url)) { $opt_v and print " as $userName ..." ; if ($ftp->login($userName,$password)) { print "\n" ; $opt_v and print "Changing to directory $sourceDir ..." ; if ($ftp->cwd($sourceDir)) { $opt_v and print "Getting $flatFile ... " ; if ($ftp->get($flatFile)) { print "done"; } else { print "failed to get file\n" ; } } else { print "failed to change directory\n" ; } } else { print "failed to login\n" ; } } else { print " connection failed\n" ; } $ftp->quit;

Thanks in advance for the help!

MadraghRua
yet another biologist hacking perl....

Replies are listed 'Best First'.
Re: Net::FTP and standard output
by petral (Curate) on Jan 03, 2002 at 04:29 UTC
    $ftp->message(); returns the responses from the other end (as an array of \n terminated lines) after each command, and $ftp->code returns the message number.

        Does Net::FTP open up a seperate shell and run the ftp session through this?

    No.  Only thing I can think, you could $|++ to autoflush STDOUT.  But it really must be somewhere else, ie., in the calling program.

      p
      how exactly does one use $ftp->message()

      i tried it once, like this

      $mess = $ftp->message(); print "$mess";

      but i either got a single digit number and nothing else or no text at all. id like to learn how to use all the Net::CMD features for Net::FTP usage, but i cant find any place that show any examples. and i learn much better from examples. :)

        Try
        @mess = $ftp->message(); print "@mess";
        I guess I pretty much just walked through the stuff using the debugger to get at the "inner" functionality.  And, yea, that "${*$ftpv}{net_cmd_resp}" stuff is strange.

          p
Re: Net::FTP and standard output
by runrig (Abbot) on Jan 03, 2002 at 04:43 UTC
    As already mentioned, use the message method (on everything but new(), and $@ for new), but also, I find something like this style more readable than all those nested if/else blocks:
    { my $ftp = Net::FTP->new(...); print("Can't connect: $@\n"), last unless $ftp; my $status = $ftp->login(...); # use 'return' instead of 'last' if it # suits your purposes better print("failed to login:",$ftp->message), last unless $status; $status = $ftp->cwd(...); print("failed to chdir:",$ftp->message), last unless $status; ... }
      Isn't this the sort of thing that eval is good for?

      eval { my $ftp = Net::FTP->new(...) or die "Can't connect: $@\n"; $ftp->login(...) or die "failed to login:".$ftp->message; $ftp->cwd(...) or die "failed to chdir:".$ftp->message; ... }; warn $@ if $@; # or whatever

      UPDATE: Shortened and fixed typo, per runrig.

          -- Chip Salzenberg, Free-Floating Agent of Chaos