next up previous contents
Next: Printing Revisited Up: File Functions Previous: Binary Files

Getting File Statistics

The file test operators can tell you a lot about a file, but sometimes you need more. In those cases, you use the stat() or lstat() function. The stat() returns file information in a 13-element array. You can pass either a file handle or a file name as the parameter. If the file can't be found or another error occurs, the null list is returned. The listing below shows how to use the stat() function to find out information about the EOF.DAT file used earlier in the chapter.

The perl code stat.pl is:

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


print("dev     = $dev\n");

print("ino     = $ino\n");

print("mode    = $mode\n");

print("nlink   = $nlink\n");

print("uid     = $uid\n");

print("gid     = $gid\n");

print("rdev    = $rdev\n");

print("size    = $size\n");

print("atime   = $atime\n");

print("mtime   = $mtime\n");

print("ctime   = $ctime\n");

print("blksize = $blksize\n");

print("blocks  = $blocks\n");

In the DOS environment, this program displays:

dev     = 2

ino     = 0

mode    = 33206

nlink   = 1

uid     = 0

gid     = 0

rdev    = 2

size    = 13

atime   = 833137200

mtime   = 833195316

ctime   = 833194411

blksize =

blocks  =

Some of this information is specific to the UNIX environmen and is not displayed here. One interesting piece of information is the $mtime value-the date and time of the last modification made to the file. You can interpret this value by using the following line of code:

($sec, $min, $hr, $day, $month, $year, $day_Of_Week, $julianDate, $dst) = localtime($mtime);

If you are only interested in the modification date, you can use the array slice notation to just grab that value from the 13-element array returned by stat().

For example:

$mtime = (stat("eof.dat"))[9];

Notice that the stat() function is surrounded by parentheses so that the return value is evaluated in an array context. Then the tenth element is assigned to $mtime. You can use this technique whenever a function returns a list.


next up previous contents
Next: Printing Revisited Up: File Functions Previous: Binary Files
dave@cs.cf.ac.uk