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

I need help from the wizards of Perl,.. I'm trying to write a small script that goes to an ftp server and "gets" some job data files, ftping them to a directory on my unix box, to a directory named after the job number,.. I then want to grab all the files in my local directory , rename them according to their contents (i.e. name is parsed from the first line of the file) (unless its already name .*memo$ or .*seq <- these dont have the filename on the firstline), Then in another subroutine I split (like unix split) the files into groups of files 600 lines or less, i.e. ignore the files smaller than 600 lines,..(both working on my local directory) Now,..the ftp part works without a hitch,..The renamefile and splitter subroutines both work according to intention,.. BUT,..here's the rub,..I cant seem to get the script to work, if BOTH subroutines are called,... i.e. runs ok if either Renamefile or Splitter is commented out,... this is the error I get and this is the script, if I dont comment out 1 or the other subroutine,... (just the ip's and user/passwords are snipped but the ftp part works fine)
> ftptest.pl Enter the COEO :=> h9z238 splitter fail : No such file or directory at ./ftptest.pl line 91, <EX +TRACT1> chunk 39.
#!/usr/bin/perl -w use strict; use File::Path; use Getopt::Std; use lib "$ENV{HOME}/perllib"; use Net::FTP; my $username = "ftpuser"; my $password = "ftppass"; my $remdir = "/webdata/extracts/SWEST/"; my @remfiles; my $remfile; my $localdir = "$ENV{HOME}/hw_extracts/"; my $coeo; my $COEO; my $coeodir; # the directory where everything happens # main # print "Enter the COEO :=> "; chomp ($coeo = <STDIN>); $COEO = uc($coeo); $coeodir = $localdir . $COEO; if (chdir($coeodir)) { chdir($localdir); rmtree($COEO) ; mkdir($COEO, 0777); chdir($coeodir);} else { chdir($localdir); mkdir($COEO, 0777); chdir($coeodir)} $remdir .= $COEO; # # try some ftping here my $ftp = Net::FTP->new("XX.XX.XX.XX") or die "Cant connect: $@\n"; # +XX is just and example $ftp->login($username, $password); $ftp->cwd($remdir); @remfiles = $ftp->ls($remdir); foreach $remfile (@remfiles) { $ftp->get($remfile); } # it works so far up to here , changes dirs etc,... # &Renamefile($coeodir); &Splitter($coeodir); ##################################################### sub Renamefile { my $extract; my $newname; my @flist; my $tempdir = shift; opendir (COEODIR, $tempdir) or die "Can't opendir $tempdir: $!"; @flist = grep { $_ ne '.' and $_ ne '..' } readdir COEODIR ; foreach $extract (@flist) { if ( $extract =~ /^.*_memo/) { $newname = $coeo."_memo"; } elsif ( $extract =~ /^.*_seq/) { $newname = $coeo."_seqchart" +; } else { open ( EXTRACT,"< $extract") || die "renamefile fail: $! +"; my @lines = <EXTRACT> ; # slurp the file into array AND my $lines = $lines[0]; # get the newfilename from the f +irst line $lines =~ /TAB (.*?)$/; $newname = $coeo . "_" . lc($1); } # close EXTRACT; rename($extract, $newname) or warn "Cant rename $extract to $newname +, $!\n"; }#foreach }#sub ##################################################### sub Splitter { my $i=0; my @arraychunk; my $extract1; my @flist; my @temparray; my $tempdir = shift; opendir (COEODIR1, $tempdir) or die "Can't opendir $tempdir: $!"; @flist = grep { $_ ne '.' and $_ ne '..' } readdir COEODIR1 ; foreach $extract1 (@flist) { open ( EXTRACT1,"< $extract1") || die "splitter fail : $!"; ## t +his is where the error comes from @temparray = <EXTRACT1> ; if ( @temparray > 600 ) { print "$extract1\n"; while (@arraychunk = splice @temparray, 0, 600 ) { open (NEWFILE, "> $extract1"."_"."$i"); $i++; print NEWFILE @arraychunk;} #while } #if # close (EXTRACT1) ; # close (NEWFILE) ; } #foreach } #sub
Thanks BusterGonad

Replies are listed 'Best First'.
Re: 2 Working Subroutines Conflict
by Vavoom (Scribe) on Oct 21, 2001 at 03:09 UTC
    The problem lies in the renaming of the file. Once you rename the file, $coeodir no longer has the correct filename, so your splitter routine is trying to open a file that does not exist. Pass the new filename to the splitter routine and it should work fine.

    Vavoom

      Or on a real OS you should be able to open a handle and then rename the file without any ill effect. You could then just pass a handle to the splitting routine.

      The splitter routine is based on directories, so how would I do this?
Re (tilly) 1: 2 Working Subroutines Conflict
by tilly (Archbishop) on Oct 21, 2001 at 03:47 UTC
    A big piece of advice.

    Pick a consistent indentation style and use it. The exact style doesn't matter so much, so long as you are consistent, the indentation makes your logical structure obvious at a glance, and your indent is in the range 2-4 characters per level of logical nesting. For instance run your script through perltidy and look at the output.

    As it stands your code is indented semi-randomly, and as a result it would be much harder to work with than it should be.

Re: 2 Working Subroutines Conflict
by slayven (Pilgrim) on Oct 21, 2001 at 01:58 UTC
    if the file doesn't exist in the local directory open will fail. so try it with the full path:
    open ( EXTRACT1,"< $tempdir/$extract1") || die "splitter fail : $!";
    erm, don't remember if $tempdir has an trailing slash in your example but IIRC UN*X filesystems don't complain if there are two of them ...
Re: 2 Working Subroutines Conflict
by data64 (Chaplain) on Oct 21, 2001 at 01:02 UTC
    Not sure if this is the cause, but you are missing calls to closedir.
      sorry,..having the #close EXTRACT, #close NEWFILE commented out doesn't make a difference,.. commented or uncommented,.. (it just happens I captured this code, through a stage of troubleshooting)