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

Hello Monks, I am new to perl and I am converting my old shell scripts over as a method of learning. I am having trouble with one script that I use to download packages that I require via ftp. Some files that I try to download end up a few bytes short. My script is:
#!/usr/bin/perl my $host = 'ftp.iinet.net.au'; my $arch = 'i386'; my $ver = '4.3'; use Net::FTP; my $package = shift; my $ftp = Net::FTP->new($host, debug => 0) or die "Cant connect: $@"; $ftp->binary(); $ftp->login("anonymous", "-anonymous@") or die "Cant login: ", $ftp->m +essage; $ftp->cwd("pub/OpenBSD/$ver/packages/$arch") or die "Cant change dir: +", $ftp->message; my @pkglist = $ftp->ls or die "Cant get dir listing: ", $ftp->message; for( @pkglist ) { my $pkg = $_; unless( $pkg =~ $package ) { next; } $pkg =~ s/\S\s{7}?//; my $test = promptUser( "Download package?", "y" ); if( $test eq "y" ) { $ftp->get( $pkg ) or die "Cannot retrieve matching package: ", + $ftp->message; } } sub promptUser { local( $promptString, $defaultValue ) = @_; if ( $defaultValue ) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; } $| = 1; $_ = <STDIN>; chomp; if ("$defaultValue") { return $_ ? $_ : $defaultValue; } else { return $_; } } exit;

Replies are listed 'Best First'.
Re: Problems with Net::FTP
by zentara (Cardinal) on Jan 05, 2009 at 16:14 UTC
    If you are missing a few bytes, you might try binary mode
    if( $test eq "y" ) { # search for binary in perldoc Net::FTP $ftp->binary(); $ftp->get( $pkg ) or die "Cannot retrieve matching package: ",

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      I was turning on binary mode straight after the call to Net::FTP->new(), but moving it inside the test, as in your reply, has indeed solved the issue. Thankyou very much for your help. :D
Re: Problems with Net::FTP
by eye (Chaplain) on Jan 06, 2009 at 07:18 UTC
    The "binary" call generates an instruction to the remote host to disable line ending translation. You cannot alter the state of the "ftp connection" until you authenticate. Hence, you need to put the "binary" call after the login, but it should not be necessary to put it inside the loop.
      You are quite correct that it not necessary to have it inside the loop. I actually figured this out myself after looking at the code again and realizing that the reason it would not work originally was because I was not connected and authenticated. So I now have the script turn on binary mode after loggin in.