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

I pass in a directory and do a stat on it. The commented out lines were returning incorrect values so I started to try and print the array. That was when I saw it showed 0. This is on Windows XP if that makes a difference.
sub DirCheck{ my $_dir = $_[0]; print "Directory: $_dir\n"; opendir (DIR, "$_dir") or die "Directory $_dir failed to open: $!" +; my @_dirStat = stat DIR; closedir (DIR); print "Meow\n"; print "DirStat: " . @_dirStat . "\n"; foreach $i (@_dirStat){ print "Item: $i\n"; } #my $_time = ctime($_dirStat[9]); #print "Last Modified Time for $_dir: $_time\n"; }

Replies are listed 'Best First'.
Re: Stat not returning values for a directory on Windows XP
by ikegami (Patriarch) on Jan 18, 2010 at 19:36 UTC
    Let's find out the reason:
    sub DirCheck { my $_dir = $_[0]; opendir(my $dh, $_dir) or die "Can't open directory $_dir: $!\n"; my @_dirStat = stat($dh) or die "Can't stat directory $_dir: $!\n"; closedir($dh); print "Directory: $_dir\n"; print "DirStat: " . @_dirStat . "\n"; for my $i (@_dirStat) { print "Item: $i\n"; } my $_mtime = $_dirStat[9]; print "Last Modified Time for $_dir: $_mtime\n"; }

    (Removed useless quotes. Avoided global vars. They weren't even localised. Cleaned up error messages.)

      Thank you. I pasted in your piece in place of what I had and got this: Script.pl died: The dirfd function is unimplemented at C:/Perl/lib/File/stat.pm line 49.

        Nothing I changed would cause that error to appear where it wasn't before. In fact, you should have gotten that error all along.

        Furthermore, it might have been worth saying that you weren't using stat. The code I gave you is wrong.

        use File::stat qw( stat ); sub DirCheck { my $_dir = $_[0]; my $_dirStat = stat($_dir) or die "Can't stat directory $_dir: $!\n"; print "Directory: $_dir\n"; for my $field (qw( dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks )) { print "$field: ", $_dirStat->$field(), "\n"; } my $_mtime = $_dirStat->mtime; print "Last Modified Time for $_dir: $_mtime\n"; }

        I also removed the useless opendir, which also avoids the error you are getting.

        Updated