True. Changing a variable in memory will not change a file. Except when using Tie::File which should be perfect for you.
Normally, to change something in a file, you need to read the file, do your change, and write the file back out.
use strict;
use warnings;
use POSIX qw( strftime );
use Text::CSV qw( );
use Tie::File qw( );
use constant INHOUSE =>"Open House Information";
{
my $today = strftime('%Y/%m/%d', localtime());
tie(my @db, 'Tie::File', 'dbase1.exm')
or die("Unable to open database: $!\n");
my $csv = Text::CSV->new();
foreach my $line (@db) {
$csv->parse($line)
or die("Unable to parse \"$line\"\n");
my @fields = $csv->fields();
my ($status, $date) = $fields[22, 23];
# Convert date to sortable format.
my ($m, $d, $y) = split(/\//, $date);
$date = "20$y/$m/$d";
my $kill = ($date < $today); # Are you sure this isn't backward
+s?
if ($status eq INHOUSE && $kill) {
$status = '';
}
# Reassemble the line and updated the file.
$fields[22] = $status;
$csv->combine(@fields)
or die("Unable to recreate line from fields @fields\n");
# Changing $line causes the file to be updated.
$line = $csv->string();
}
untie @db;
}
If you're trying to remove the entire line, it's even easier:
use strict;
use warnings;
use POSIX qw( strftime );
use Text::CSV qw( );
use Tie::File qw( );
use constant INHOUSE =>"Open House Information";
{
my $today = strftime('%Y/%m/%d', localtime());
tie(my @db, 'Tie::File', 'dbase1.exm')
or die("Unable to open database: $!\n");
my $csv = Text::CSV->new();
@db = grep {
$csv->parse($line)
or die("Unable to parse \"$line\"\n");
my @fields = $csv->fields();
my ($status, $date) = $fields[22, 23];
# Convert date to sortable format.
my ($m, $d, $y) = split(/\//, $date);
$date = "20$y/$m/$d";
$status ne INHOUSE || $date >= $today
} @db;
untie @db;
}
|