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

I am trying to get the modification time of files within a directory, however, using File::stat is coming back blank. What could cause this? Here is an example:
use File::stat; opendir(DIR, "/to/my/directory") or die("Cannot open directory"); my @files= readdir(DIR); closedir DIR; foreach my $file (@files) { my $mod = (stat($file))[9]; print "Filename $file has the mod date of $mod \n"; }
This outputs to "Filename, whatever, has the mod date of " Meaning, $mod is blank. I have tried both full directory of /to/my/directory/filename as well and this turns blank too. It seems that "stat" is returning nothing. Any reason this is the case?

UPDATE: It seems that actually saying use File::stat is the culript.... when commenting this out, I get an epoch time. Why the heck is this the case? Is File::stat not correctly where I assumed it was?

Replies are listed 'Best First'.
Re: Perl Stat Issue
by CountZero (Bishop) on Jun 26, 2013 at 14:59 UTC
    If you use the File::stat module then stat returns an object that you have to access with the proper methods. Please read the docs of this module.

    use Modern::Perl; use File::stat; opendir( DIR, 'c:/data' ) or die('Cannot open directory'); my @files = readdir(DIR); closedir DIR; foreach my $file (@files) { my $full_name = 'c:/data/' . $file; my $st = stat($full_name); my $mod = $st->mtime; say "Filename $full_name has the mod date of $mod"; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Update: It seems that actually saying use File::stat is the culript.... when commenting this out, I get an epoch time. Why the heck is this the case? Is File::stat not correctly where I assumed it was?
        Well, you added the use stat; line so you should have read that module's documentation which says:
        This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. This object has methods that return the similarly named structure field name from the stat(2) function; namely, dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Perl Stat Issue
by Corion (Patriarch) on Jun 26, 2013 at 14:31 UTC
      again, comes back blank when using full directory, not just filename. even if I do something as simplistic as:
      use File::stat; my $testfile = "/to/my/directory/test.file"; my $st = stat($testfile) or die "No $testfile: $!";
      $st is completely blank...
Re: Perl Stat Issue
by thundergnat (Deacon) on Jun 26, 2013 at 14:43 UTC

    The files are not in the current directory. You need to supply the path to stat as well.

    Probably something like:

    use File::stat; my $path = '/to/my/directory'; opendir(DIR, $path) or die("Cannot open directory"); my @files= readdir(DIR); closedir DIR; foreach my $file (@files) { my $mod = (stat("$path/$file"))[9]; print "Filename $file has the mod date of $mod \n"; }
      I tried this. It also comes back completely blank, as if File::stat has some critical error even though it seems up to date. This is my output. $stat is just not working. What is the best way to diagnose this?
      Use of uninitialized value in concatenation (.) or string at test.pl l +ine 11. Filename test.txt has the mod date of
Re: Perl Stat Issue
by Laurent_R (Canon) on Jun 26, 2013 at 17:59 UTC

    File::stat is not returning what you think (check the documentatioon). If you want to use this syntax:

    my $mod = (stat($file))[9];

    you should probably use the core stat functionnality, rather than the module.

Re: Perl Stat Issue
by daxim (Curate) on Jun 26, 2013 at 14:46 UTC
      No, the script would fail outright :)