如何在 perl 中使用 File::stat 获取文件的本地时间修改?
How can I get the local time modification of a file with File::stat in perl?
如何获取本地时间格式的文件修改时间?
通过这样做:
use File::stat;
use Time::Piece;
my $format = '%Y%m%d%H%M';
print Time::Piece->strptime(stat($ARGV[0])->mtime, '%s')->strftime($format);
我收到 202011301257
的文件,该文件是在我当地时间 (GMT+01:00) 的 11 月 30 日 13:57 保存的。
既然我能做到
print localtime $file->stat->mtime;
和
print localtime->strftime($format)
我想做类似的事情
print (localtime stat($file)->mtime)->strftime($format);
哪个抛出
Can't locate object method "mtime" via package "1" (perhaps you forgot to load "1"?)
有什么建议吗?
I'd like to do something like
print (localtime stat($file)->mtime)->strftime($format);
非常接近!你的第一个括号在错误的位置:
#!/usr/bin/env perl
use warnings; # Pardon the boilerplate
use strict;
use feature 'say';
use File::stat;
use Time::Piece;
my $format = '%Y%m%d%H%M';
say localtime(stat($ARGV[0])->mtime)->strftime($format);
一直用use strict; use warnings;
. 会发现问题:
print (...) interpreted as function at a.pl line 6.
您有以下内容
print ( localtime ... )->strftime($format);
因为print
和(
之间的space没有意义,上面等价于下面的:
( print( localtime ... ) )->strftime($format);
问题是您在 print
的结果上使用了 ->strftime
。如果您不省略 print
操作数周围的括号,问题就会消失。
print( ( localtime ... )->strftime($format) );
或者,不省略括号 localtime
的参数将允许您删除引起问题的括号。
print localtime( ... )->strftime($format);
如何获取本地时间格式的文件修改时间?
通过这样做:
use File::stat;
use Time::Piece;
my $format = '%Y%m%d%H%M';
print Time::Piece->strptime(stat($ARGV[0])->mtime, '%s')->strftime($format);
我收到 202011301257
的文件,该文件是在我当地时间 (GMT+01:00) 的 11 月 30 日 13:57 保存的。
既然我能做到
print localtime $file->stat->mtime;
和
print localtime->strftime($format)
我想做类似的事情
print (localtime stat($file)->mtime)->strftime($format);
哪个抛出
Can't locate object method "mtime" via package "1" (perhaps you forgot to load "1"?)
有什么建议吗?
I'd like to do something like
print (localtime stat($file)->mtime)->strftime($format);
非常接近!你的第一个括号在错误的位置:
#!/usr/bin/env perl
use warnings; # Pardon the boilerplate
use strict;
use feature 'say';
use File::stat;
use Time::Piece;
my $format = '%Y%m%d%H%M';
say localtime(stat($ARGV[0])->mtime)->strftime($format);
一直用use strict; use warnings;
. 会发现问题:
print (...) interpreted as function at a.pl line 6.
您有以下内容
print ( localtime ... )->strftime($format);
因为print
和(
之间的space没有意义,上面等价于下面的:
( print( localtime ... ) )->strftime($format);
问题是您在 print
的结果上使用了 ->strftime
。如果您不省略 print
操作数周围的括号,问题就会消失。
print( ( localtime ... )->strftime($format) );
或者,不省略括号 localtime
的参数将允许您删除引起问题的括号。
print localtime( ... )->strftime($format);