Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Reading from file- not change atime

by Khen1950fx (Canon)
on Dec 10, 2013 at 09:44 UTC ( #1066408=note: print w/ replies, xml ) Need Help??


in reply to Reading from file- not change atime

When opening a filehandle, use utime:

#!/usr/bin/perl use strict; use warnings; no warnings qw(uninitialized); use File::stat; my $file = shift or die $!; open my $fh, '<', $file or die $!; my ($atime, $mtime) = (stat($fh))[8,9]; utime($atime, $mtime, $fh) or die "couldn't restore $file to original times: $!"; close $fh;


Comment on Re: Reading from file- not change atime
Download Code
Replies are listed 'Best First'.
Re^2: Reading from file- not change atime
by Eliya (Vicar) on Dec 10, 2013 at 15:45 UTC

    Actually, you shouldn't load the module File::stat when you say (stat($fh))[8,9], because the module overrides the stat builtin, and the overridden stat returns an object providing methods such as atime, mtime, etc., instead of a list of values...

    In other words, either simply don't use File::stat, or say something like

    my $st = stat($fh) or die $!; my ($atime, $mtime) = ($st->atime, $st->mtime); ...

    or even (if you like):

    use File::stat ":FIELDS"; ... stat($fh) or die $!; my ($atime, $mtime) = ($st_atime, $st_mtime); ...

    (Of course, you could also do away with the intermediate variables, and put the method calls directly in utime's argument list, i.e. utime($st->atime, $st->mtime, $fh), in case you're using File::stat).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1066408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (13)
As of 2015-07-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls