in reply to I don't want to die

eval can catch the dies:
while (defined (my $file = readdir($DH))) { my $cretime; eval {$cretime=stat("$SPOOL/$file")->ctime; 1;} or next; push(@files, [$cretime,$file]); }
Note: the 1; is added to ensure the eval returns true on success.

Replies are listed 'Best First'.
Re^2: I don't want to die
by JadeNB (Chaplain) on Jul 29, 2009 at 23:29 UTC
Re: I don't want to die
by moodwrench (Initiate) on Jul 29, 2009 at 15:46 UTC
    Thanks.. eval works perfectly. I used ctime as I want the time that the file was added to the directory by another script. So far ctime seems to return the correct files and my script continues to run after the eval
    Thanks again!

      Thanks.. eval works perfectly.

      Not really. Why cause an error and then silence *all* errors when it's easier to not cause it in the first place?

      my ($cretime) = eval { stat("$SPOOL/$file")->ctime } or next; push @files, [ $cretime, $file ];
      vs
      my $stat = stat("$SPOOL/$file") or next; push @files, [ $stat->ctime, $file ];

      I used ctime as I want the time that the file was added to the directory by another script.

      ctime definitely doesn't return what you want.

      $ perl -MFile::stat -le'print "".localtime( stat("a")->ctime )' Wed Jul 29 12:13:21 2009 $ chmod u+r a $ perl -MFile::stat -le'print "".localtime( stat("a")->ctime )' Wed Jul 29 12:13:46 2009
      As ikegami points out, UNIX based systems don't actually store creation time anywhere. A better bet is usually to have the script that adds the files to the directory append the timestamp to the end of the file name.