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 information about the file. How can I get its last modified time stamp?
相关问题
- $ENV{$variable} in perl
- Is it possible to pass command-line arguments to @
- Lazily Reading a File in D
- Redirecting STDOUT and STDERR to a file, except fo
- Change first key of multi-dimensional Hash in perl
相关文章
- How to replace file-access references for a module
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Why is file_get_contents faster than memcache_get?
- Transactionally writing files in Node.js
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
Use the builtin stat function. Or more specifically:
You need the stat call, and the file name:
Perl also has a different version:
but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.
The modification time is stored in Unix format in $array[9].
Or explicitly:
The epoch was at 00:00 January 1, 1970 GMT.
More information is in stat.
If you're just comparing two files to see which is newer then
-C
should work: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.You can use the built-in module
File::stat
(included as of Perl 5.004).Calling
stat($fh)
returns an array with the following information about the file handle passed in (from the perlfunc man page forstat
):The 9th element 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:
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). This requires some (arguably) more legible code:You could use stat() or the File::Stat module.