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

Hello everyone, I have done some research on writting an FTP program for my Intranet site. I have ran across CGI.pm and NET::FTP. What I am wondering is which of the two is more reliable and if anyone has a good script that I could follow to write my own? Your help would be greatly appreciated. - Bionicle32

Replies are listed 'Best First'.
Re: FTP Program
by BazB (Priest) on Oct 23, 2003 at 20:55 UTC

    I'd think that Net::FTP would be ever so slightly more applicable to an FTP-based application than CGI.
    The documentation for Net::FTP is comprehensive - you should be able to produce a working script using the examples and information there.

    It doesn't matter how "reliable" something is if it's designed for a completely different application.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: FTP Program
by Roger (Parson) on Oct 24, 2003 at 00:50 UTC
    Here's a stripped down version of my script to fetch production data from FTP server, hope this would put you on the right track.

    #!/usr/local/bin/perl -w # Fetch the latest production data use Getopt::Long; use Pod::Usage; use Net::FTP; use strict; # Parse command line arguments and assign corresponding variables GetOptions ( 'd|date=s' => \( my $date = undef ), 'v|verbose' => \( my $VERBOSE = 0 ), 'r|root=s' => \( my $root = '.' ), ); unless ( defined $date ) { pod2usage( -exitval => 1, -output => \*STDERR ); } # Create the local directories -d "$root/data" or mkdir("$root/data"); # Establish an FTP connection my $ftp = Net::FTP->new("prod", Debug => 0) or die "Cannot connect to the production box: $@"; $ftp->login("user","passwd") or die "Cannot login ", $ftp->message; &FetchRemoteFiles($ftp, "/opt/data", $date, "$root/data"); $ftp->quit; exit(0); # Fetch files under given directory sub FetchRemoteFiles { my ($ftp, $dir, $date, $localdir) = @_; $ftp->cwd($dir) or die "Cannot change working directory ", $ftp->message; my @remote_files = $ftp->ls("*.txt"); my $latest_date; if (uc $date eq "LATEST"){ $latest_date = @{[reverse sort map {/(\d{8})/;$1} @remote_files]}[ +0]; } else { $latest_date = $date; } print "Fetching data for $latest_date...\n" if $VERBOSE; foreach my $file (@remote_files){ if ($file =~ /$latest_date/){ print ">> $file\n" if $VERBOSE; $ftp->get($file, "$localdir/$file") or die "Can not fetch file ", $ftp->message; } } print "Ok!\n" if $VERBOSE; } sub Today() { my ($day, $month, $year) = (localtime)[3,4,5]; return sprintf "%04d%02d%02d", $year + 1900, $month+1, $day; } __END__ =pod =head1 NAME fetchdata.pl =head1 SYNOPSIS fetchdata.pl [options] =head1 ARGUMENTS All arguments except date to this script are optional. =over 4 =item B<-d|--date [YYYYMMDD|latest]> Specify the date of data to fetch from production. =item B<-r|--root [directory]> Specify the root directory to store the data files. Under the director +y there are two sub directories, aus and asia. =item B<-v|--verbose> Let the script print out more debug information. =back =cut
Re: FTP Program
by vek (Prior) on Oct 24, 2003 at 03:17 UTC

    Net::FTP would certainly be the way to go for writing your FTP program. CGI has nothing to do with FTP; it's the Perl implementation of the Common Gateway Interface. A quick browse of the CGI.pm documention would have told you that. Remember, documentation is your friend.

    -- vek --
Re: FTP Program
by castaway (Parson) on Oct 24, 2003 at 06:56 UTC
    You didn't specify if you meant an FTP server or client. Net::FTP is a client implementation, which you can use to login to another FTP server to get/put files etc. If you want to run an FTP server in perl, you could try Net::FTPServer.

    C.