How do I get a file's last modified time in Perl?

后端 未结 9 885
说谎
说谎 2020-12-13 03:36

Suppose I have a filehandle $fh. I can check its existence with -e $fh or its file size with -s $fh or a slew of additional informati

相关标签:
9条回答
  • 2020-12-13 03:53

    If you're just comparing two files to see which is newer then -C should work:

    if (-C "file1.txt" > -C "file2.txt") {
    {
        /* Update */
    }
    

    There's also -M, but I don't think it's what you want. Luckily, it's almost impossible to search for documentation on these file operators via Google.

    0 讨论(0)
  • 2020-12-13 03:54
    my @array = stat($filehandle);
    

    The modification time is stored in Unix format in $array[9].

    Or explicitly:

    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
        $atime, $mtime, $ctime, $blksize, $blocks) = stat($filepath);
    
      0 dev      Device number of filesystem
      1 ino      inode number
      2 mode     File mode  (type and permissions)
      3 nlink    Number of (hard) links to the file
      4 uid      Numeric user ID of file's owner
      5 gid      Numeric group ID of file's owner
      6 rdev     The device identifier (special files only)
      7 size     Total size of file, in bytes
      8 atime    Last access time in seconds since the epoch
      9 mtime    Last modify time in seconds since the epoch
     10 ctime    inode change time in seconds since the epoch
     11 blksize  Preferred block size for file system I/O
     12 blocks   Actual number of blocks allocated
    

    The epoch was at 00:00 January 1, 1970 GMT.

    More information is in stat.

    0 讨论(0)
  • 2020-12-13 03:59

    You need the stat call, and the file name:

    my $last_mod_time = (stat ($file))[9];
    

    Perl also has a different version:

    my $last_mod_time = -M $file;
    

    but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.

    0 讨论(0)
  • 2020-12-13 04:01

    Use the builtin stat function. Or more specifically:

    my $modtime = (stat($fh))[9]
    
    0 讨论(0)
  • 2020-12-13 04:07

    You could use stat() or the File::Stat module.

    perldoc -f stat
    
    0 讨论(0)
  • 2020-12-13 04:08

    Calling the built-in function stat($fh) returns an array with the following information about the file handle passed in (from the perlfunc man page for stat):

      0 dev      device number of filesystem
      1 ino      inode number
      2 mode     file mode  (type and permissions)
      3 nlink    number of (hard) links to the file
      4 uid      numeric user ID of file's owner
      5 gid      numeric group ID of file's owner
      6 rdev     the device identifier (special files only)
      7 size     total size of file, in bytes
      8 atime    last access time since the epoch
      9 mtime    last modify time since the epoch
     10 ctime    inode change time (NOT creation time!) since the epoch
     11 blksize  preferred block size for file system I/O
     12 blocks   actual number of blocks allocated
    

    Element number 9 in this array will give you the last modified time since the epoch (00:00 January 1, 1970 GMT). From that you can determine the local time:

    my $epoch_timestamp = (stat($fh))[9];
    my $timestamp       = localtime($epoch_timestamp);
    

    Alternatively, you can use the built-in module File::stat (included as of Perl 5.004) for a more object-oriented interface.

    And to avoid the magic number 9 needed in the previous example, additionally use Time::localtime, another built-in module (also included as of Perl 5.004). Together these lead to some (arguably) more legible code:

    use File::stat;
    use Time::localtime;
    my $timestamp = ctime(stat($fh)->mtime);
    
    0 讨论(0)
提交回复
热议问题