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

I have a script that compares the mtime of a file with timelocal set back 24 hours.
If the mtime is less then the timelocal it removes the file.
I am trying to modify it so we have the option to save files whatever number of days back we would like.
The subroutine is below the way it is now it does not reduce the timelocal to less then previous day file's mtime.
How should I do the calculation so it reduces the timelocal back more then one day?
sub clean_up() { my ( $mtime ) = ( stat $_ )[9]; my $BACKUP_TIME = $params->get("Backup.Tape.Start"); my $SERVER = $params->get("Backup.Server"); my $SAVE_DAYS = 1; my $SAVE_DAYS = $params->get("Backup.Save.Days") if ( $params->exists( +"Backup.Save.Days") ); my ( $BACKUP_HOUR, $BACKUP_MIN ); if ( $BACKUP_TIME =~ /^\d{1,2}:\d{1,2}$/ ) { ( $BACKUP_HOUR, $BACKUP_MIN ) = split /:/, $BACKUP_TIME; } else { $app->log($WARN, "Parameter \"Backup.Tape.Start\" not formatted pr +operly [ HH:MM ]. Defaulting to 07:00"); ( $BACKUP_HOUR, $BACKUP_MIN ) = ( 07, 00 ); } my ($min, $hour, $mday, $month, $year ) = (localtime)[1,2,3,4,5]; my $time = timelocal(0,$BACKUP_MIN,$BACKUP_HOUR,$mday,$month,$year); ##Set time to yesterday if current time of day < configured back-up t +ime $time -= ((60 * 60) * (24 * $SAVE_DAYS)) if ( $hour < $BACKUP_HOUR || ( $hour == $BACKUP_HOUR && $min < $BA +CKUP_MIN )); if ( $mtime < $time ){ print "$_\n"; } #unlink $_ if ( $mtime < $time ); }

Replies are listed 'Best First'.
Re: Setting timelocal back by days
by shmem (Chancellor) on Sep 15, 2007 at 10:22 UTC
    Maybe it just works if your correct
    if ( $time < $time ){ print "$_\n"; }
    to compare $time to $mtime.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      typo
      if ( $time < $time ){ print "$_\n"; }
      Correct
      if ( $mtime < $time ){ print "$_\n"; }
      However it still unlinks files that were created after the Save.Days time. Any file newer then 5 days ago should not get unlinked but it does anyway. The $time is not getting set properly.