cpan intermediate

How to get file modification time with Path::Tiny in Perl?

Question

How to get file modification time with Path::Tiny in Perl?

How to Get File Modification Time with Path::Tiny in Perl

Path::Tiny is a powerful and lightweight module for manipulating files and directories in Perl. Among its many features, it provides an easy way to get metadata about files, including the modification time (mtime).

To get the modification time of a file using Path::Tiny, you typically call the stat method on a Path::Tiny object. This method returns a list of file attributes similar to the built-in stat function, and the modification time is the 9th element (index 9 in zero-based arrays).

Key Points

  • mtime stands for "modification time" and represents the last time the file content was modified.
  • Path::Tiny->stat returns the full stat info as a list, same as Perl's core stat.
  • The 10th element (index 9) of the list is modification time, expressed as epoch seconds (Unix timestamp).
  • localtime() can be used to convert this epoch time into a human-readable string.
  • Path::Tiny requires Perl 5.8.1 or higher (but generally used with modern Perls).

Example: Get and Print File Modification Time

use strict;
use warnings;
use Path::Tiny;

# Path to the file
my $file_path = "example.txt";

# Create the Path::Tiny object
my $file = path($file_path);

# Check if file exists before calling stat
if ($file->exists) {
    # Get stat info, returns list: dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, blocks
    my @stat = $file->stat;

    # Modification time is the 10th element (index 9)
    my $mtime = $stat[9];

    # Convert to human-readable time
    my $human_mtime = localtime($mtime);

    print "File: $file_path\n";
    print "Modification time (epoch): $mtime\n";
    print "Modification time (human): $human_mtime\n";
} else {
    print "File '$file_path' does not exist.\n";
}

Explanation

In this example, path($file_path) creates a Path::Tiny object. The stat method returns a list of filesystem-related values, exactly like Perl's built-in stat. The modification time is at position 9 in this list and is in seconds since the epoch (January 1, 1970).

By wrapping localtime($mtime), you convert this epoch number to a human-friendly format like Thu Feb 1 12:34:56 2024. This makes it easy to display or process modification times.

Common Pitfalls and Gotchas

  • File existence: Always verify the file exists with exists before calling stat to avoid warnings or errors.
  • Index confusion: Remember that stat returns a list with fixed element positions; modification time is not just [9] but the tenth element (zero-based index 9).
  • Context matters: Since stat returns a list, avoid calling it in scalar context expecting a single value.
  • Timezone: localtime converts epoch time using your system’s local timezone. Use gmtime for UTC.

Summary

Using Path::Tiny to get file modification time combines simplicity with the flexibility of stat. Just create a file object, call stat, extract the 10th element for mtime, and convert it with localtime for a readable date.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 27ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
File: example.txt
Modification time (epoch): 
Modification time (human): Thu Jan  1 08:00:00 1970
STDERR
Use of uninitialized value $mtime in localtime at - line 20.
Use of uninitialized value $mtime in concatenation (.) or string at - line 23.

Was this helpful?

Related Questions