在Perl中的dirhandle上,'stat'的语义是什么?

时间:2022-10-11 12:15:58

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).

在研究另一个问题时,我注意到Perl中的stat函数可以将dirhandle作为其参数(而不是文件句柄或文件名)。

However I can't find any examples of correct use of this - there are none in the Perl manual.

但是我找不到任何正确使用它的例子 - Perl手册中没有。

Can anyone show an example of how to use it?

谁能展示如何使用它的例子?

4 个解决方案

#1


8  

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.

在Perl 5.10上添加了在目录句柄上使用stat的能力,因此如果你关心可移植性应该避免使用它。

#2


2  

You use it just like you would stat on a filehandle:

你就像在文件句柄上使用stat一样使用它:

<~> $ 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...)

(就个人而言,我喜欢使用File :: stat来获得很好的命名访问器,所以我不必记住(或查找)第五个元素是UID ......)

#3


1  

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:

请注意,如果一个句柄曾被用作文件句柄,以及一个dirhandle,则stat将应用于该文件,而不是目录:

$ 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?)

#4


0  

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.

我在Windows上使用Perl 5.10.1(ActivePerl)并且在dirhandle上执行stat不起作用。但是在目录的路径字符串上执行stat工作。

works

  my $mtime = (stat( $directory ))[ 9 ];
  print "D $directory $mtime\n";

this doesn't ("The dirfd function is unimplemented...")

  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";

#1


8  

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.

在Perl 5.10上添加了在目录句柄上使用stat的能力,因此如果你关心可移植性应该避免使用它。

#2


2  

You use it just like you would stat on a filehandle:

你就像在文件句柄上使用stat一样使用它:

<~> $ 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...)

(就个人而言,我喜欢使用File :: stat来获得很好的命名访问器,所以我不必记住(或查找)第五个元素是UID ......)

#3


1  

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:

请注意,如果一个句柄曾被用作文件句柄,以及一个dirhandle,则stat将应用于该文件,而不是目录:

$ 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?)

#4


0  

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.

我在Windows上使用Perl 5.10.1(ActivePerl)并且在dirhandle上执行stat不起作用。但是在目录的路径字符串上执行stat工作。

works

  my $mtime = (stat( $directory ))[ 9 ];
  print "D $directory $mtime\n";

this doesn't ("The dirfd function is unimplemented...")

  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";