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?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.