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

This code returns the correct epoch value on some files and 0 on others in the same directory. Can anyone help?
opendir (REPORT, "reports/") || &ErrorMessage; foreach $filename (readdir REPORT) { next if $filename =~ /^\.\.?$/; $modify = (stat($filename))[10]; push(@modifyreport, $modify); } closedir (REPORT);

Replies are listed 'Best First'.
Re: Why does stat work sporadically?
by japhy (Canon) on Feb 27, 2001 at 05:02 UTC
    Because readdir() doesn't return relative path-names. It returns directory entries. Try stat "reports/$filename" next time.
Re: Why does stat work sporadically?
by mr.nick (Chaplain) on Feb 27, 2001 at 06:18 UTC
    Probably because the 10th element in the array returned by stat() is actually the change time of the inode (which probably isn't available under Win32), not the modification time of the file.
    
      0 dev      device number of filesystem
      1 ino      inode number
      2 mode     file mode  (type and permissions)
      3 nlink    number of (hard) links to the file
      4 uid      numeric user ID of file's owner
      5 gid      numeric group ID of file's owner
      6 rdev     the device identifier (special files only)
      7 size     total size of file, in bytes
      8 atime    last access time since the epoch
      9 mtime    last modify time since the epoch
     10 ctime    inode change time (NOT creation time!) since the epoch
     11 blksize  preferred block size for file system I/O
     12 blocks   actual number of blocks allocated
    

    You probably want

    $modify=(stat($filename))[9];
Re: Why does stat work sporadically?
by Anonymous Monk on Feb 27, 2001 at 05:18 UTC
    Thanks for the help. I've been looking through my books and newsgroup postings for three days and get the answer here in a matter of minutes. I had tried file modification time and it hadn't worked either. The os is unix. japhy's answer of stat "reports/$filename" did the trick.

      In addition to your books and newsgroups, you might want to add searching through the bundled, online documentation as part of your looking-things-up routine. In particular, perlfunc says, under the entry for readdir:

      If you're planning to filetest the return values out of a readdir(), you'd better prepend the directory in question. Otherwise, because we didn't chdir() there, it would have been testing the wrong file.
Re: Why does stat work sporadically?
by dws (Chancellor) on Feb 27, 2001 at 05:02 UTC
    What operating system are you running this one, and why do you want the inode change time (10) rather than the file modification time (9)?

    Originally posted as a Categorized Answer.

Re: Why does stat work sporadically?
by dws (Chancellor) on Feb 27, 2001 at 05:08 UTC
    (retracted)

    Originally posted as a Categorized Answer.