[Chapter 10] 10.7 The stat Function
Author: Randal L. Schwartz, Erik Olson & Tom Christiansen
Date: 1999-07-02T01:34:35Z

#

| Chapter 10 Filehandles and File Tests |

## 10.7 The stat Function

While these file tests are fine for testing various attributes regarding a particular file or filehandle, they don't tell the whole story. To get at the remaining information about a file, merely call the `stat` function, which returns pretty much everything that the POSIX system call `stat` returns (hopefully more than you want to know). Not all of the `stat` fields are meaningful under Perl for Win32, because they include information not supported by the Windows NT filesystems.

The operand to `stat` is a filehandle or an expression that evaluates to a filename. The return value is either `undef`, indicating that the stat failed, or a 13-element list,[10] most easily described using the following list of scalar variables:

> [10] If you have a hard time remembering the order of `stat`'s return values, you might look at the `File::stat` module, first introduced in release 5.004 of Perl. It provides access such as:

>     $file_owner = stat($filename)->uid;

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

Table 10.2 lists each field along with a brief description.

Table 10.2: stat Return Valves
Field | Description
--- | ---
dev | Device number (drive number)
ino | Inode number: 0 (zero) in Perl for Win32
mode | File permission mode: read/write/execute
nlink | Number of links to file (usually one for Win32 systems - NTFS filesystems may have a value greater than one)
uid | User ID - zero for Win32
gid | Group ID - zero for Win32
rdev | Device Identifier (drive number)
size | File size in bytes
atime | Last access time (C lang. time_t value)
mtime | Last modification time (C lang. time_t value)
ctime | File creation time (C lang. time_t value)
blksize | Disk block size (cluster size): zero for Win32
blocks | Number of blocks for file: zero for Win32

Like the file tests, the operand of `stat` defaults to `$_`, meaning that the stat will be performed on the file named by the scalar variable `$_`.

You can retrieve information about the filesystem of the current active drive using the `Win32::FsType` function:

    $fstype = Win32::FsType;
    if ($fstype =~ /NTFS/) {
        print "NTFS -- good choice!\n";
    }

10.6 The -x File Tests |  | 10.8 Exercises