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

Code snippet requested, so here's a bit:
$file = "\/etc\/hosts"; $t1 = -A $file; $t2 = stat( $file );

The stat() call seems to reset not only its own clock, but also the clock used by -A; it also reads its own calling as a file access.

After the stat() call, which returns "right now" as its own answer, any calls to the -A return zero days--an indication to me that stat() has reset the clock.

I don't know if it matters--though it probably does--but this is all being attempted on a UNIX system.

Thanks again!
Deane

Edited - Steve_p removed BR tags from inside the code

Replies are listed 'Best First'.
stat($f) vs -A $f
by Dismas (Acolyte) on Jul 28, 2004 at 20:11 UTC
    Wizards,
    I'm trying to track down "the last time a file was used." The -A op seems to give what I need. But...
    While experimenting with stat() I noticed that it seems to not only reset whatever thingie the -A op uses, but that it also sees *itself* as a "file access," so that every file I run stat($f) on returns just under 1.1 billion seconds--which translates roughly to right now today. Unless it reads the -A as a file access. In either case, if I run one or the other on four-year-old files, after running both I get a zero from -A and the 1.1 billion seconds from stat().
    So, what can you in your great wisdom tell me about stat(), about -A, whatever they use, reset, and otherwise foul up?
    Thanks!
    Reply to: Deane.RothenmaierREMOVETHIS@Walgreens.com

    20070729 Edit by ysth: reparented this SoPW under its followup

      That seems odd. What's your OS and version of Perl? On my RH9 default install of Perl
      #!/usr/bin/perl ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks)=stat("/tmp/foo.txt"); print localtime($atime) . "\n";
      Returns the last accessed time and does not count as a file access.

      Does your script do anything else with these files?

      Could you post some sample code that shows the strange behavior?

      Keep in mind that -A is the script startup time (which can be found in $^T) minus the file access time--in days. stat returns the absolute last access time in seconds since the epoch.

      The following gets the last access time from a file by both methods. I tried it on three different OSs (OS X, WinXP, and RH 9), and the results are the same.

      # do it with stat my $atime = (stat($ARGV[0]))[8]; print "stat says: ", scalar localtime $atime, "\n"; # do it with -A $atime = $^T - (-A $ARGV[0]) * 86400; print "-A says: ", scalar localtime $atime, "\n";

      Brad

Re: More on Stat() vs -A
by borisz (Canon) on Jul 29, 2004 at 14:11 UTC
    I think you did not call stat right. to get the accesstime for the file you need to call stat like this:
    $t2 = (stat( $file ))[8];
    and -A gives the accesstime in days from the start of the script! to convert it back to a accesstime like the one from -A you have to $^T  - (-A $file) * 24 * 60 * 60;
    And the whole example:
    $file = "\/etc\/hosts"; $t1 = -A $file; $t2 = (stat( $file ))[8]; $t3 = $^T - (-A $file) * 24 * 60 * 60; print $t1, $/; print $t2, $/; print $t3, $/;
    Boris
Re: More on Stat() vs -A
by Joost (Canon) on Jul 29, 2004 at 14:06 UTC
    $file = "/etc/exports"; # <!-- note the filename $t1 = -A $file; $t2 = localtime((stat( $file ))[8]); print "t1 =$t1, t2=$t2\n"; __END__ t1 =72.6357060185185, t2=Tue May 18 00:54:13 2004
    Seems fine to me. HOWEVER, it appears that my /etc/hosts file is accessed for just about any network address my machine connects with (at least for every page I open in firefox), so that might be the cause of your problem.

    update: Just to close the loophole here, I'd like to demonstrate that calling -A after stat() does not give a different result:

    $file = "/etc/exports"; $t1 = -A $file; $t2 = localtime((stat( $file ))[8]); $t3 = -A $file; print "t1 =$t1, t2=$t2, t3=$t3\n"; __END__ t1 =72.6449537037037, t2=Tue May 18 00:54:13 2004, t3=72.6449537037037
Re: More on Stat() vs -A
by bgreenlee (Friar) on Jul 29, 2004 at 15:50 UTC

    FYI, you can also use File::stat to be able to reference the various stat fields by name:

    use File::stat; print stat('/some/file')->atime;

    Brad