stat FILEHANDLE
stat EXPR
stat DIRHANDLE
stat

返回一个 13 个元素的列表,提供通过 FILEHANDLE 或 DIRHANDLE 打开的文件或由 EXPR 命名的文件的状态信息。如果省略 EXPR,它将统计 $_(不是 _!)。如果 stat 失败,则返回空列表。通常按如下方式使用

my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
    $atime,$mtime,$ctime,$blksize,$blocks)
       = stat($filename);

并非所有字段都受所有文件系统类型支持。以下是字段的含义

 0 dev      device number of filesystem
 1 ino      inode number
 2 mode     file mode  (type and permissions)
 3 nlink    number of (hard) links to the file
 4 uid      numeric user ID of file's owner
 5 gid      numeric group ID of file's owner
 6 rdev     the device identifier (special files only)
 7 size     total size of file, in bytes
 8 atime    last access time in seconds since the epoch
 9 mtime    last modify time in seconds since the epoch
10 ctime    inode change time in seconds since the epoch (*)
11 blksize  preferred I/O size in bytes for interacting with the
            file (may vary from file to file)
12 blocks   actual number of system-specific blocks allocated
            on disk (often, but not always, 512 bytes each)

(纪元为 1970 年 1 月 1 日格林尼治标准时间 00:00。)

(*)并非所有字段都受所有文件系统类型支持。值得注意的是,ctime 字段不可移植。特别是,您不能期望它是一个“创建时间”;有关详细信息,请参阅 "perlport 中的“文件和文件系统”

如果 stat 传递了由下划线组成的特殊文件句柄,则不会执行 stat,但会返回来自上一次 statlstat 或文件测试的 stat 结构的当前内容。示例

if (-x $file && (($d) = stat(_)) && $d < 0) {
    print "$file is executable NFS file\n";
}

(这仅适用于在 NFS 下设备号为负数的机器。)

在某些平台上,inode 号码的类型比 perl 能够作为整数数值值处理的类型更大。如有必要,inode 号码将作为十进制字符串返回,以保留整个值。如果在数字上下文中使用,这将被转换为浮点数值,并进行舍入,最好避免这种情况。因此,您应该更愿意使用 eq 而不是 == 来比较 inode 号码。eq 对于以数字表示的 inode 号码以及以字符串表示的 inode 号码都能正常工作。

由于模式同时包含文件类型及其权限,因此如果您想查看实际权限,则应该屏蔽文件类型部分并使用 "%o" 进行 (s)printf。

my $mode = (stat($filename))[2];
printf "Permissions are %04o\n", $mode & 07777;

在标量上下文中,stat 返回一个布尔值,指示成功或失败,并且如果成功,则设置与特殊文件句柄 _ 关联的信息。

File::stat 模块提供了一种方便的按名称访问机制

use File::stat;
my $sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;

您可以从 Fcntl 模块导入符号模式常量 (S_IF*) 和函数 (S_IS*)

use Fcntl ':mode';

my $mode = (stat($filename))[2];

my $user_rwx      = ($mode & S_IRWXU) >> 6;
my $group_read    = ($mode & S_IRGRP) >> 3;
my $other_execute =  $mode & S_IXOTH;

printf "Permissions are %04o\n", S_IMODE($mode), "\n";

my $is_setuid     =  $mode & S_ISUID;
my $is_directory  =  S_ISDIR($mode);

您可以使用 -u-d 运算符编写最后两个。

# Permissions: read, write, execute, for user, group, others.

S_IRWXU S_IRUSR S_IWUSR S_IXUSR
S_IRWXG S_IRGRP S_IWGRP S_IXGRP
S_IRWXO S_IROTH S_IWOTH S_IXOTH

# Setuid/Setgid/Stickiness/SaveText.
# Note that the exact meaning of these is system-dependent.

S_ISUID S_ISGID S_ISVTX S_ISTXT

# File types.  Not all are necessarily available on
# your system.

S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR
S_IFIFO S_IFSOCK S_IFWHT S_ENFMT

# The following are compatibility aliases for S_IRUSR,
# S_IWUSR, and S_IXUSR.

S_IREAD S_IWRITE S_IEXEC

常用的 S_IF* 常量为

S_IMODE($mode)    the part of $mode containing the permission
                  bits and the setuid/setgid/sticky bits

S_IFMT($mode)     the part of $mode containing the file type
                  which can be bit-anded with (for example)
                  S_IFREG or with the following functions

# The operators -f, -d, -l, -b, -c, -p, and -S.

S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)

# No direct -X operator counterpart, but for the first one
# the -g operator is often equivalent.  The ENFMT stands for
# record flocking enforcement, a platform-dependent feature.

S_ISENFMT($mode) S_ISWHT($mode)

并且 S_IF* 函数为

有关 S_* 常量的更多详细信息,请参阅本机 chmod(2)stat(2) 文档。若要获取符号链接的状态信息,而不是链接背后的目标文件,请使用 lstat 函数。