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


In reply to Comparing file properties by mcoblentz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.