How can I get the file modification time formatted in local time?
By doing this:
use File::stat;
use Time::Piece;
my $format = '%Y%m%d%H%M';
print Time::Piece->strptime(stat($ARGV[0])->mtime, '%s')->strftime($format);
I get 202011301257
for a file that was saved at Nov 30 13:57 in my local time (GMT+01:00).
Since I can do
print localtime $file->stat->mtime;
and
print localtime->strftime($format)
I'd like to do something like
print (localtime stat($file)->mtime)->strftime($format);
Which throws
Can't locate object method "mtime" via package "1" (perhaps you forgot to load "1"?)
Any advice?
I'd like to do something like
print (localtime stat($file)->mtime)->strftime($format);
Very close! Your first parenthesis is in the wrong spot:
#!/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);
Re "Your first parenthesis is in the wrong spot", That's not actually the problem. The placement of the parenthesis is correct. The problem is that the omitted parens around print are required here because
print (...)->strftime($format);
means( print(...) )->strftime($format);