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

Thanks everyone! I eventually ended up just rotating the logs at a known time and working it that way. If only the year was included in this program's output. :)

Replies are listed 'Best First'.
Re: Dates! Log Files! Help!
by ikegami (Patriarch) on Nov 22, 2004 at 17:26 UTC
    use strict; use warnings; use Time::Local (); my $file_name = shift(@ARGV); open(LOG_FH, '<', $file_name) or die("...$/"); open(TMP_FH, '>', $file_name.'.tmp') or die("...$/"); # Might want to clear the hours, minutes and secs from $old. my $old = time - 90*24*60*60; while (<LOG_FH>) { my ($y, $m, $d) = /...(...)...(...)...(...).../; my $date = Time::Local::timelocal(0, 0, 0, $d, $m-1, $y); last if $date > $old; } do { print TMP_FH $_; } while (<LOG_FH>); close(LOG_FH); close(TMP_FH); unlink($file_name); rename($file_name.'.tmp', $file_name);

    Replace ...s with appropriate code. Off the top of my head. There may be a better way. Doesn't lock. Untested.

    Actually, Forget everything I said and run Logfile::Rotate as a cron job every 90 days.

    Update: I forgot that you specified the date format. Time::ParseDate would probably be of more use than Time::Local.

      Nice solution!

      The only other thing that came to mind was using Date::Manip to recognize and parse the date. I haven't used it in a long time, but when I did I remember one of it's strengths is being able to recognize dates formatted in many different ways. The one drawback was that it was slow if using it to parse LOTS of dates. Just something to think about.

Re: Dates! Log Files! Help!
by Grygonos (Chaplain) on Nov 22, 2004 at 17:25 UTC

    Due to the date format you are using it is not always possible to get a correct truth value. If you have the date Dec 31. You may not throw it out correctly. Say you check the date Dec 31 on Jan 1, you algorithm would show a date distance of 1 day, but maybe these are more than 1 day apart (i.e. Dec 31 1942 and Jan 1 2005) you have no way of knowing since year is not listed in your log files. What is writing these log files? If you are I suggest reformatting them properly, and then taking a look at The DateTime Module

    Also for deleting lines from the file.. you can't "technically" do that.. you can blank out lines but it will still be left as blank. You should open a new filehandle and write to it the things that pass your 90 day test.