I am trying to modify an already existing piece of code. This piece of
code -> opens a db file -> split the records (one of which is date when the
record was added) -> compare the dates with current date -> and remove all
the records if those are old enteries (as specified to remove e.g 7 days old
enteries). So the database gets updated automatically and all the old
enteries are deleted.
Now what I am trying to do is to append deleted enteries to another file
too. In case, If I need to access older enteries I can access that archived
file.
Any ideas?
Thanks for you help.
Sara.
# DB FILE #
John|06-Nov-2003
Doe|05-Nov-2003
Smith|04-Nov-2003
Sara|03-Oct-2003
Deiley|02-Oct-2003
#
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI;
my $q = new CGI;
my $db_file_name = 'test.txt';
my $remove = 3; # Number of days old.
my $today = &date_to_delete(&get_delete_date);
my $removeby = $today - ($remove * 86400);
open (READ, "$db_file_name") || die "unable to open DB FILE $!";
my @lines = ;
close (READ);
print $q->header();
open (DB, ">$db_file_name") || die "can't open the file again $!";
foreach my $line(@lines) {
chomp $line;
my ($name, $id);
($name, $id) = split (/\|/, $line);
if ($removeby > &date_to_delete($id)){next;}
print DB "$line\n";
}
close DB;
print "Data Updated - Old Enteries Deleted!";
sub get_delete_date {
#
# Returns the date in the format "dd-mmm-yy".
# Warning: If you change the default format, you must also modify the
&date_to_unix
# subroutine below which converts your date format into a unix time in
seconds for sorting
# purposes.
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) =
localtime(time());
my (@months) = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
($day < 10) and ($day = "0$day");
$year = $year + 1900;
return "$day-$months[$mon]-$year";
}
sub date_to_delete {
#
# This routine must take your date format and return the time a la UNIX
time().
# Some things to be careful about..
# int your values just in case to remove spaces, etc.
# catch the fatal error timelocal will generate if you have a bad date..
# don't forget that the month is indexed from 0!
#
my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4,
"Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);
my ($time);
my ($day, $mon, $year) = split(/-/, $_[0]);
unless ($day and $mon and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal(0,0,0,$day, $months{$mon}, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}
##
P.S. The above two sub-routines are taken from: DBMan -
http://www.gossamer-threads.com/
#
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]