Perl stat - How to determine the access or modification time of a file

How to use the Perl stat function.

Problem: Using Perl, you need to determine the last time a file was accessed (read) or updated (modified).

Solution: Use the Perl stat function to get this information. Let's look at a couple of short examples to see how this works.

Perl stat - getting the file access time

To determine the last access (read) time of a file named foo.txt, use this sample Perl code:

$filename = 'foo.txt';
$last_access_time = (stat($filename))[8];
print "$last_access_time\n";

Perl stat - the file update/modification time

To determine the last modification time of a file named foo.txt, use this sample Perl code:

$filename = 'foo.txt';
$last_update_time = (stat($filename))[9];
print "$last_update_time\n";

More Perl stat information

As shown in the Perl stat link, here are the definitions of these two fields:

8 atime  last access time in seconds since the epoch
9 mtime  last modify time in seconds since the epoch

Again, there is much more information in that stat link.