I'm working on
tieing a hash to a mySQL database using some modified code from Conway's "OOP". When the hash is tied and a special key is set, it loads the data from the database appropriately. It seems that DESTROY doesn't like saving my changes. I have the following:
package Test;
my @STRUCTURE = qw(name test);
my $TABLENAME = "test";
my $KEY = "id";
*DBH = *main::DBH;
.
.
sub DESTROY {
my($impl) = @_;
my $keyval = $impl->{$KEY}->{value};
my @list;
my $time = time;
push(@list,"timestamp='$time'");
foreach(@STRUCTURE){
my $val = $impl->{$_}->{value};
$val =~ s/'/\\'/g;
push(@list,"$_='$val'");
}
my $string = join ",",@list;
$DBH->do("
UPDATE $TABLENAME
SET $string
WHERE $KEY=$keyval
LIMIT 1
");
}
If you set it to print the code, it does so and displays valid mySQL syntax. I find it interesting that
die doesn't work within DESTROY, but I guess I could understand that. My tied hash is going out of scope by default when the namespace is cleared at the end of the script's execution. Why is it not achieving the desired effect?