Reputation: 339816
On researching another question I noted that the stat
function in Perl can take a dirhandle as its argument (instead of a filehandle or filename).
However I can't find any examples of correct use of this - there are none in the Perl manual.
Can anyone show an example of how to use it?
Upvotes: 1
Views: 4186
Reputation:
I use Perl 5.10.1 on windows (ActivePerl) and doing stat on a dirhandle doesn't work. But doing a stat on the path string of the directory works.
my $mtime = (stat( $directory ))[ 9 ];
print "D $directory $mtime\n";
my $dh;
if( opendir( $dh, $directory ) == 0 ) {
print "ERROR: can't open directory '$directory': $!\n";
return;
}
$mtime = (stat( $dh ))[ 9 ];
print "D $directory $mtime\n";
Upvotes: 0
Reputation: 98398
Just be aware that a if a handle was ever used as a filehandle, as well as a dirhandle, the stat will apply to the file, not the directory:
$ perl -wl
opendir $h, "." or die;
open $h, "/etc/services" or die;
print "dir:".readdir($h);
print "file:".readline($h);
print stat("/etc/services");
print stat(".");
print stat($h);
close($h);
print stat($h);
__END__
dir:.
file:# Network services, Internet style
205527886633188100018274122800783211967194861209994037409640
20551515522168777410001000020480122803711512280371021228037102409640
205527886633188100018274122800783211967194861209994037409640
stat() on closed filehandle $h at - line 1.
(Are you trying to call stat() on dirhandle $h?)
Upvotes: 1
Reputation: 109022
You use it in the same way you do for a file or filehandle:
#!/usr/bin/perl
use strict;
my $dir = shift;
opendir(DIR, $dir) or die "Failed to open $dir: $!\n";
my @stats = stat DIR;
closedir(DIR);
my $atime = scalar localtime $stats[8];
print "Last access time on $dir: $atime\n";
The ability to use stat on directory handles was just added around Perl 5.10 so it should be avoided if you care about portability.
Upvotes: 8
Reputation: 140728
You use it just like you would stat
on a filehandle:
<~> $ mkdir -v foo ; perl -e 'opendir($dh , "./foo"); @s = stat $dh; print "@s"'
mkdir: created directory `foo'
2049 11681802 16877 2 1001 1001 0 4096 1228059876 1228059876 1228059876 4096 8
(Personally, I like using File::stat
to get nice named accessors, so that I don't have to remember (or lookup) that the fifth element is the UID...)
Upvotes: 2