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

Hello Gentlemen, I am using the below code to get the last modified time of a file, but somehow the output is just plain blank:
use File::stat; $i = "/tmp/file1.txt"; $mtime = (stat($i))[9]; print "Last modified time is $mtime\n";
Grateful for your kind thoughts on this.

Replies are listed 'Best First'.
Re: not able to get last modified time of a file
by Ratazong (Monsignor) on Jan 28, 2011 at 07:22 UTC

    stat is a built-in-function ... you don't need to use File::stat.

    Even "worse": if you look at the docu for File::stat, you'll see

    This module's default exports override the core stat() and lstat() functions
    so the built-in function won't work any longer. Simply remove your "use"-line from your script to solve your issue.

    HTH, Rata

      So,

      (stat($qfn))[9]

      or

      use File::stat; stat($qfn)->mtime

      or

      use File::stat; (CORE::stat($qfn))[9] # Bypass override by File::stat
Re: not able to get last modified time of a file
by ack (Deacon) on Jan 28, 2011 at 16:07 UTC

    The module stats is a built-in function of Perl as Ratazong points out; so you don't need File::stats and as Ratagong also points out, using File::stats causes the built-in stats to be overridden which is probably not what you'd want overall...but that's just MHO.

    The stats() function returns an array reference, not the array itself. So you'll have to derefference the result of stat() before you then try to access element 9. That is what I believe ikegami was showing you.

    Also, in your included code snippet you left out the brackets around 9...but I presume that was just an oversight in what you posted, not as it occured in your code.

    UPDATE: I inadvertantly posted that the fuction is stats(). It should be stat() I believe. Sorry.

    UPDATE 2: Well, now I'm confused. When I was trying the case that the OP posted, I found that stat() returned an array reference (hence, my posted response). But when I was playing around with it a little further, I find that, indeed, I can use the construc my $mtime = (stat($i))[9] to get the last modified time. And I can't get it to return the array reference now. So now I'm a bit confused as this implies that the OP's only mistake, as far as I can tell, was that there were no enclosing square brackets around his array element number. Once I corrected that, the OP's code works for me.

    ack Albuquerque, NM