buz260 has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I have previously been using -M $filename to get the timestamp (how long ago the file $filename was last updated):
if (-M $outputfile < $timeforfiletoupdate_beforeerror) { # etc }
However I'd like to be able to store this value in a variable rather than just use it in conditional statments... Is there a way of doing this? Thanks

Replies are listed 'Best First'.
Re: Getting a file's timestamp and storing this as a variable
by gwadej (Chaplain) on Apr 16, 2009 at 13:29 UTC

    Unless I'm missing something obvious...

    my $timestamp = -M $outputfile;

    Very often, in Perl, a good answer is to type the code that would work if Perl did what you want. Often, it does.

    G. Wade
Re: Getting a file's timestamp and storing this as a variable
by ikegami (Patriarch) on Apr 16, 2009 at 14:02 UTC

    -M is just a weirdly named function, really.

    if (-M $outputfile < $timeforfiletoupdate_beforeerror) { # etc }

    is the same as

    if (-M($outputfile) < $timeforfiletoupdate_beforeerror) { # etc }

    And like every other function, nothing's stopping you from assigning its result to a variable.

    $days_since = -M $outputfile;