Perl Programming Page 3 - File Tests in Perl |
Though these file tests are fine for testing various attributes regarding a particular file or filehandle, they don’t tell the whole story. For example, there’s no file test that returns the number of links to a file or the owner’s user ID (uid). To get at the remaining information about a file, call the stat function, which returns pretty much everything that the stat Unix system call returns (and more than you want to know).† The operand to stat is a filehandle or an expression that evaluates to a filename. The return value is either the empty list indicating that thestatfailed (usually because the file doesn’t exist), or a 13-element list of numbers, most easily described using the following list of scalar variables: my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, The names here refer to the parts of the stat structure, described in detail in thestat(2)manpage. You should look there for the detailed descriptions. Here’s a quick summary of the important ones: $dev and $ino
$mode
$nlink
$uid and $gid
$size
$atime, $mtime, and $ctime
Invoking stat on the name of a symbolic link returns information on what the symbolic link points at and not information about the symbolic link itself unless the link happens to be pointing at nothing currently accessible. If you need the (mostly useless) information about the symbolic link itself, use lstat rather thanstat(which returns the same information in the same order). If the operand isn’t a symbolic link,lstatreturns the same things thatstat would. Like the file tests, the operand ofstatorlstatdefaults to$_, meaning the underlying stat system call will be performed on the file named by the scalar variable$_.
blog comments powered by Disqus |