in reply to Infinite Loop Question

for (;;;)
will work, though as rthhawk says
while(1)
is maybe a little more conventional.There's no inbuilt limit to the loop either way.
As to why it ends:
  • Some other process/es on the host chews up the cpu?
  • Some other process/es on the host gorges the memory?
  • Your LOG target gets zapped by some other agent?
  • Other?

  • Hard to say.

    Options?

    You could set up the script to run under some process monitor or other. There are quite a few around. For a bit of fun you could write your own (but who monitors the monitor?).

    If you wrap the whole thing in an eval you maybe able to trap the condition that causes the exit and resume the loop.

    See eval documentation.

    Replies are listed 'Best First'.
    Re^2: Infinite Loop Question
    by joeymac (Acolyte) on Nov 09, 2011 at 13:13 UTC

      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!

        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!