如何获取以本地时间格式化的文件修改时间?
这样做:
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
,该文件保存在当地时间11月30日13:57(GMT+01:00(。
既然我能做
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"?)
有什么建议吗?
<blockquote\我想做一些类似的事情
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
和(
之间的空间是没有意义的,所以以上等价于以下内容:
( print( localtime ... ) )->strftime($format);
问题是您正在对print
的结果使用->strftime
。如果不省略print
操作数周围的parens,问题就会消失。
print( ( localtime ... )->strftime($format) );
或者,如果不省略parenslocaltime
的args,则可以删除导致问题的parens。
print localtime( ... )->strftime($format);