I must first say, that's not going to be accurate. Even as your hardware likely supports nanosecond resolution by the time it gets back to software it's hundreds of times off. (With a file's timestamp, presumably used only as relative to other files, that may be less important.)
Having said that, the only way I find to claim nanoseconds is to use system's tools, so stat
my ($ts) = grep { /^\s*Access: [0-9]{4}/ } qx(stat $file);
$ts =~ s/^\s*Access:\s+//;
or, using map
also as a filter
my ($ts) = map { /^\s*Access:\s+([0-9]{4}.*)/ ? $1 : () } qx(stat $file);
One problem here, of course, is that we have to parse output; so see your man pages and test your stat
, and don't hope for much portability. Watch for system changes as output format of programs may change. But I don't find nanoseconds claim in Perl.
For reference, on my CentOS 7 (under both bash
and tcsh
)
perl -we'$f=shift; print grep { /^\s*Access: [0-9]{4}/ } qx(stat $f)' file
prints, for a random file
Access: 2019-05-15 13:21:57.723987422 -0700
Once again, the "nano"seconds aren't accurate at all.
Another option, for perhaps more reliable output parsing, is to use ls
with its --full-time
my $ts = join ' ', (split ' ', qx(ls --full-time $file))[5..7];
As always, when running external commands build your variables with care and quote and escape appropriately. A good tool is String::ShellQuote. Or, better avoid the shell altogether unless it's specifically needed. Best do all this using a module for running external tools.