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

I am really, really new to Perl. I have been writing a script the processes a batch of web orders (sequential files) and reformats them to be transfered (FTP) to another system. My problem is, I can't seem to write decent code to do the job. I researched and found that NET::FTP is a good way to get it done but it is not installed on the server my script is running on nor can I ever get it installed. So, I have this snippet of code that supposedly would work with NET::FTP. It is supposed to read through a directory and transfer all of the files located there ( the only files will be the ones that I previously processed ) to a specific server; then deleting the processed files. Are there any Perl geniuses out there that can re-write my psuedo Perl using NET::FTP into code that just uses standard Perl components? Also, If anybody could catch any errors in my code snippet I would be very appreciative ( I obviously can't test it ).

Now, the majority of my script works and the reformatted files are placed into a directory ( lets call it /output ). This code snippet is the last, untested, part that uses NET:FTP to transfer the files out of that directory.

# read through the directory containing all of the formatted # .ord files processed above and ftp them to SAP. delete the # files when after they have been transfered. use NET::FTP; my $hostname='secret.server.mycompany.com; my $user='anonymous'; my $password='anonymous'; my $ftp=Net::FTP -> new($hostname); ftp->login($username,$password); $ftp->ascii; chdir( "output/$dir" ); opendir( DIR, "output/$dir" ); @files = readdir( DIR ); # transfer all of the output files # in the output directory to SAP. foreach( @files ) { $filehandle = $_; $ftp->put("output/$filehandle"); unlink( "$filehandle" ); } # end foreach $ftp->quit;

Please remember that I am only a student and am still trying to learn. I know there are probably a gazillion better ways to do everything I have written and I would be glad to learn them all. Thanks for the help :)

Replies are listed 'Best First'.
Re: Alternative to NET::FTP
by swiftone (Curate) on Jun 20, 2000 at 00:15 UTC
      O.K., I did that and I think that it is almost working but my script is not actually finding the module..

      I get an error: Can't call "new" in empty package "Net:FTP"

      I have tried playing around with the directory listing in the use lib statement but nothing seems to work.

      Here is a refined version of my original code with some of the bugs removed:

      use lib "script/libnet/Net/FTP.pm"; $ftp = Net::FTP->new("ftp.mycompany.com"); $ftp->login("anonymous","anonymous"); $ftp->ascii; chdir( "$dir/$output" ); opendir( DIR, "$dir/output" ); @files = readdir( DIR ); # transfer all of the output files # in the output directory to SAP. foreach( @files ) { $filehandle = $_; $ftp->put("output/$filehandle"); unlink( "$filehandle" ); } # end foreach $ftp->quit;
        Using a module that's not installed in the normal places Perl looks always takes an extra step:
        use lib '/your/module/directory';
        After you do that, Perl will look in that directory for any subsequent modules you load. You still have to load Net::FTP:
        use Net::FTP; # after the earlier use statement
        So you need both those steps (point Perl at the right place, then load the module). You can't compress it into one, as you're trying to do.

        Ahh, merlyn is being a little coy. You want: use lib <path to library>; Don't specify the particular module. You import that with: use <module name>; In the case of Net::FTP, you can translate the double colons into a directory separator. And your 'use lib' statement ought to point to the directory in which Perl should start looking for Net::FTP (on Unix systems, Perl looks in each of the library locations for Net/FTP.pm).

        Get the picture? You're giving it too much information, and it's looking for script/libnet/Net/FTP.pm/Net/FTP.pm.

        You could use require, but try trimming your path and you'll have better luck. (Oh, and you also might want to use Net::FTP somewhere before you call Net::FTP::new(), too.)

      O.K., I did that and I think that it is almost working but my script is not actually finding the module..

      I get an error: Can't call "new" in empty package "Net:FTP"

      I have tried playing around with the directory listing in the use lib statement but nothing seems to work.

      Here is a refined version of my original code with some of the bugs removed:

      use lib "script/libnet/Net/FTP.pm"; $ftp = Net::FTP->new("ftp.mycompany.com"); $ftp->login("anonymous","anonymous"); $ftp->ascii; chdir( "$dir/$output" ); opendir( DIR, "$dir/output" ); @files = readdir( DIR ); # transfer all of the output files # in the output directory to SAP. foreach( @files ) { $filehandle = $_; $ftp->put("output/$filehandle"); unlink( "$filehandle" ); } # end foreach $ftp->quit;
Re: Alternative to NET::FTP
by c-era (Curate) on Jun 20, 2000 at 00:28 UTC
    Are you interested in learning the FTP proto, or do you want something that someone else wrote?