Click to See Complete Forum and Search --> : stat a directory using Perl


bsamuels
12-14-2003, 08:20 PM
Below is a code snippet that I used as a test (it's part of a larger program) to stat the contents of a directory. The problem is that it only works sometimes, on some files it successfully retrieves the entire stat list but on others it doesn't. This is especially true when going to another directory other than the working directory. Though this file uses the File::stat module, I get the same results in versions without. I'm confused, it seems easy enough. Can someone help.?

#!/usr/bin/perl
use File::stat;
$file = shift (@ARGV);
if( -d $file){
opendir MYDIR, $file or die"Cannot open $file: $!";
print "$file:\n";
while ($dirfiles = readdir MYDIR){
$filestats = stat($dirfiles);
print scalar localtime $filestats->mtime," $dirfiles\n";
next if $dirfiles =~ /^\./;
}
}

jim mcnamara
12-15-2003, 11:59 AM
I've been away from Perl for years - but

use Library 'modulename'

is the correct syntax.

Also, just coding
use File
is what you want to do in this case - opendir is part of File I think.