mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:
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:
If I try it with just mapping $sb onto mtime (the lines that are commented out at 'stat', I get: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$
So now I'm stumped. I have come to the monastery seeking 'enlightenment'. Help me, my brothers!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$
Matt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Comparing file properties
by arnaud99 (Beadle) on Jun 26, 2013 at 16:19 UTC | |
|
Re: Comparing file properties
by Laurent_R (Canon) on Jun 26, 2013 at 17:46 UTC | |
by mcoblentz (Scribe) on Jun 27, 2013 at 00:44 UTC | |
by kcott (Archbishop) on Jun 27, 2013 at 08:08 UTC |