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

Dear Monks,

I am trying to determine the age of a file by comparing its last modified time to the current time. Naturally, I have hit a wall with this one because the mtime I'm managing to extract is either a string or something in a weird array. This is probably simple but I'm beating my head on the keyboard.

The simple script I'm using is this:

#!/opt/local/bin/perl use strict; use warnings; use File::stat; # initialize my $element; my $filename; my $fh; my @files; my $sb; my @quake_times; my $color; my $cur_time = time; my $mtime; print ("current time is ", $cur_time, "\n" ); @quake_times = qw( 21600 3600); @files = qw( markers/quake markers/volcano satellites/NORAD.tle); my $directory = "/Users/coblem/xplanet/"; foreach $element (@files) { $filename = $directory . $element; open $fh, '<', $filename; #$sb = stat($fh); # scalar localtime $sb->mtime; $mtime = (stat($fh))[9]; print ($cur_time , " ", $mtime); if (($cur_time - $mtime) > @quake_times[1]) { $color = 'red'; } elsif (($cur_time - $mtime) > @quake_times[0]) { $color = 'yellow'; } else { $color = 'green'; } print ("color is ", $color, "\n"); }

And the terminal results I get are:

Last login: Tue Jun 25 21:03:59 on ttys000 usxxcoblemm1:~ coblem$ cd testing usxxcoblemm1:testing coblem$ ls Global_24h.csv csv_loading.pl hello_world Quakes filestats.pl hello_world.pl change_blue_marble.pl filestats_2.pl volcano_script.sh clouds_4096.sh fire.csv usxxcoblemm1:testing coblem$ perl filestat2_2.pl Can't open perl script "filestat2_2.pl": No such file or directory usxxcoblemm1:testing coblem$ perl filestats_2.pl Scalar value @quake_times[1] better written as $quake_times[1] at file +stats_2.pl line 36. Scalar value @quake_times[0] better written as $quake_times[0] at file +stats_2.pl line 39. current time is 1372261642 Use of uninitialized value $mtime in print at filestats_2.pl line 34. Use of uninitialized value $mtime in subtraction (-) at filestats_2.pl + line 36. 1372261642 color is red Use of uninitialized value $mtime in print at filestats_2.pl line 34. Use of uninitialized value $mtime in subtraction (-) at filestats_2.pl + line 36. 1372261642 color is red Use of uninitialized value $mtime in print at filestats_2.pl line 34. Use of uninitialized value $mtime in subtraction (-) at filestats_2.pl + line 36. 1372261642 color is red usxxcoblemm1:testing coblem$
If I try it with just mapping $sb onto mtime (the lines that are commented out at 'stat', I get:
Scalar value @quake_times[1] better written as $quake_times[1] at file +stats_2.pl line 36. Scalar value @quake_times[0] better written as $quake_times[0] at file +stats_2.pl line 39. current time is 1372261739 1372261739 File::stat=ARRAY(0x7f8fc902d7b0)color is green 1372261739 File::stat=ARRAY(0x7f8fc9055238)color is green 1372261739 File::stat=ARRAY(0x7f8fc9055190)color is green usxxcoblemm1:testing coblem$
So now I'm stumped. I have come to the monastery seeking 'enlightenment'. Help me, my brothers!

Matt

Replies are listed 'Best First'.
Re: Comparing file properties
by arnaud99 (Beadle) on Jun 26, 2013 at 16:19 UTC

    Hi

    Not using File::Stat, and using the 'core' stat, may be one solution

    use Modern::Perl; my @files = qw { ./adt1.pl ./adt2.pl }; my $color; my $cur_time = time; my @quake_times = qw( 21600 3600); say 'Current time: ', $cur_time; foreach my $a_filename(@files) { #stat() on the file name my $mtime = (stat($a_filename))[9]; say $a_filename, ", " , $mtime; if (($cur_time - $mtime) > $quake_times[1]) { $color = 'red'; } elsif (($cur_time - $mtime) > $quake_times[0]) { $color = 'yellow'; } else { $color = 'green'; } say ("color is ", $color, "\n"); }

    The code above outputs:

    ~/perl/ex/perlmonks: perl ./adt5.pl Current time: 1372263479 ./adt1.pl, 1372261299 color is green ./adt2.pl, 1372262161 color is green

    I hope this helps

    Arnaud

Re: Comparing file properties
by Laurent_R (Canon) on Jun 26, 2013 at 17:46 UTC

    Why are you not simply using the -M -A or -C file test operators which return directly the age of the file in days (and fractions of days)? It seems that's doing exactly what you need.

    -M : last modification

    -A : last access

    -c : last inode change

      Well, that is exactly what I am looking for. -M it is!

      Thank you!

        The documentation for the file test operators is in perlfunc under -X, which can be a little tricky to track down unless you were specifically looking for the -X operator.

        Alternatively, from the commandline, it's a little easier (although these still all take you the the same doco):

        $ perldoc -f -M $ perldoc -f -A $ perldoc -f -C $ perldoc -f -X

        -- Ken