in reply to Re: Infinite Loop Question
in thread Infinite Loop Question

What I did was write a quick script to send STDERR to a log file. Things went astray again and this is the message that I got.

 fileparse(): need a valid pathname at ftpData.pl line 312

I am using fileparse (File::Basename) to disregard any file extensions in directory comparisons and to remove the existing extension when the file is copied and renamed with a different extension, like so:

#Disregard file extension in comparison: foreach my $item (@wrkDirList) { my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.]*/); $item = $fileName; }

and

foreach $item (@filesToRetrv) { my ( $fileName, $filePath, $fileExt ) = fileparse($item, qr/\.[^.] +*/); if (rename($item, $fileName)) { push (@filesToMove, $fileName); } else { print LOG "Rename failed for $localName to $fileName\n"; } }

So, and correct me if I am wrong, somewhere along the line it looks like fileparse is getting passed an empty filehandle? I'm not really sure how this could happen. The STDERR message seems to be occurring at the second instance of fileparse (the one working on @filesToRetrv). Here is a little snippet with info about this array and what goes into it:

foreach $fileToGet (@matches) { my $remoteFileSize = $ftp->size($fileToGet); $localFileName = "$fileToGet".".xxx"; $ftpReturnVar = $ftp->get($fileToGet, $localFileName); $localFileSize = (stat "$workingDir/$localFileName")[7]; if ($remoteFileSize == $localFileSize) { push (@filesToRetrv, $ftpReturnVar); } else { ... } }

Any ideas? Thanks for the help!

Replies are listed 'Best First'.
Re^3: Infinite Loop Question
by mrstlee (Beadle) on Nov 09, 2011 at 15:16 UTC
    One thing lest the PBP fundamentalists get you - always use a lexical variable in foreach loops:
    foreach my $fileToGet (@matches) { ...
    What happens if $ftp->get fails? You may want to check whether you get a valid return value e.g.
    $ftpReturnVar = $ftp->get($fileToGet, $localFileName); defined $ftpReturnVar or next;

      This is actually probably quite helpful! Thanks! I added the line:

      next if ! defined $ftpReturnVar;

      mostly due to personal preference of having an "if" in my conditional statements and restarted to script. It has been running now for several of hours without failure. I will see how it is doing after 24 hours or so. Thanks again for your advice!