Hi all,
I'm new to perl (that's why I'm here :-). I'm building a few cgis allowing
me to add, modify, delete users in a text file (perl version 5.8.0 on a
redhat 8 machine).
So far, I've been able to create the form and the perl scripts to add
users and verify their existence.
I'm now trying to delete one user/record inside my text file.
You will find below the script I wrote.
Actually the temporary file is created the way it should be but I didn't
succeed in renaming it to users.dat, saying "no such file or directory".
This should be not possible as the script succeeds in opening users.dat
and reading from it.
I know that a nicer solution is to use Tie::File but I have to say that
from the documentation I found I don't really understand how to do it :-(
Thanks in advance for any help
Gael
# value we will read from the html form
my $name=$ARGV[0];
$data_file="users.dat";
# we will read the database record by record, copy each recor to the
temporary file
# and simply forget to write the record to be deleted.
$tmp_file="users.tmp";
open(DAT, "<$data_file") || die ("Could not open users file!");
open(TMPDAT, ">$tmp_file") || die ("Could not open temporary file!");
while (<DAT>)
{
# extract the username (the first field) from the record
my ($username)=split(/\|/,$_);
# test the name agains the username
if ($name eq $username) {
# we've found the record to delete, so skip it and move to next record
next;
}
# write the original record out to the temporary file
print TMPDAT $_ or die "Error writing $tmp_file: $!\n";
}
close DAT or die ("closing the users file!\n");
close TMPDAT or die ("closing $temp_file: $!\n");
# we delete the old file
unlink $data_file or die ("Can't delete old $data_file: $!\n");
# and rename the new file to replace the old one.
rename ($temp_file, $data_file) or die ("Can't rename '$tmp_file' to
'$data_file': $!\n");