Igor Oks Igor Oks - 5 months ago 23
Perl Question

How can I get a file's modification date in DDMMYY format in Perl?

What's the best way to get a file's modification date in DDMMYY format in Perl?

(Originally the question was "How to do it without the DateTime module". I removed this restriction in order to make the question more general.)

Answer

If this is the only date formatting I need to do and don't need any date features for anything else, I just use POSIX (which comes with Perl):

use POSIX;

my $file = "/etc/passwd";

my $date = POSIX::strftime( 
             "%d%m%y", 
             localtime( 
                 ( stat $file )[9]
                 )
             );

Forget about -M if you aren't using relative time. Go directly to stat to get the epoch time.