Hello,
I am trying to make this sub write either to the begining or the end of the file. The variable $placement indicates which process will execute. This variable is then used in the if statement inside the sub routine below.
The print statement print DB "$record\n";
prints to the end of the file. How do I print to the top of the file.
Thanks
In the begining
Let us first assume that there was nothing to begin with.
I am trying to make this sub write either to the begining or the end of the file. The variable $placement indicates which process will execute. This variable is then used in the if statement inside the sub routine below.
The print statement print DB "$record\n";
prints to the end of the file. How do I print to the top of the file.
Thanks
Code:
sub add_record {
$key = time();
$record=$key;
$placement = $q->param('placement');
foreach $field (@fields){
${$field} = $q->param($field);
${$field} = filter(${$field});
$record .= "\::${$field}";
}
unless (-e $database){
open (DB, ">$database") || die "Error creating database. $!\n";
} else {
open (DB, ">>$database") || die "Error opening database. $!\n";
}
flock DB, $EXCLUSIVE;
seek DB, 0, 2;
if ($placement eq "beg") {
} else { print DB "$record\n";
}
flock DB, $UNLOCK;
close(DB);
}
# End of add_record subroutine.
Let us first assume that there was nothing to begin with.