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 傳遞一個由底線組成的特殊檔案代號,不會執行統計,但會傳回最後一次 statlstat 或檔案測試的 stat 結構的目前內容。範例

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

(這只適用於在 NFS 下裝置編號為負數的機器。)

在某些平台上,inode 號碼的類型比 perl 作為整數數值值處理的類型還要大。如有必要,inode 號碼會以十進位字串傳回,以保留整個值。如果用在數字脈絡中,這會轉換成浮點數值,並進行四捨五入,最好避免這種情況。因此,您應該偏好使用 eq 而不是 == 來比較 inode 號碼。eq 會在 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 算子寫出最後兩個。常見的 S_IF* 常數為

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

請參閱您本機的 chmod(2)stat(2) 文件,以取得有關 S_* 常數的更多詳細資料。若要取得符號連結的狀態資訊,而不是連結背後的目標檔案,請使用 lstat 函式。

移植性問題:perlport 中的「stat」