Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

My script uses url_param's from a link to delete certain hash keys. This has never been a problem before but for some reason it's now screwing everything up. If there are three things in the hash and you delete one by pressing the link, it will delete but one of the remaining hash keys will corrupt and have a null value.

I can't for the life of me figure out what the problem of this could be. From the snippet below does anyone have any ideas?

Thanks so much for your time.

my $rem = url_param("rem"); use DB_File; my %default; my $default = "default.db"; # location of database tie %default, "DB_File", "$default", O_CREAT|O_RDWR, 0644, or die "Oop +s! $!\n"; print header, start_html; if ($rem ne "") { if (exists $default{$rem}) { delete $default{$rem}; print "<center><font color=blue>The URL was removed successfully +.</font></center>"; } else { print "<center><font color=red>That URL was not found.</font></c +enter>"; } }

Replies are listed 'Best First'.
Re: Hash key removal problems
by sgifford (Prior) on May 04, 2004 at 16:00 UTC

    This code doesn't have locking, so if two people run it at once the database file could become corrupt. If's now being run more often than before, the odds of this happening will go up, and that could be why you're just starting to see it.

    I would try locking the database handle, and see if the problem goes away.

Re: Hash key removal problems
by eserte (Deacon) on May 04, 2004 at 16:18 UTC
    Berkeley database files often tend to get corrupt. Sometimes they are still readable, but write operations fail. You can check by calling the del method instead of using the delete statement and looking at the return status value. To restore a corrupted berkeley database, you can just db_dump and db_load the file.
      There isn't anything in the DB right now. I setup a few test keys and delete them. And when it doesn't work properly I delete the .db and start again. So the DB can't be corrupted because I always have a new one. del didn't seem to remove the hash key.
Re: Hash key removal problems
by dragonchild (Archbishop) on May 04, 2004 at 15:57 UTC
    This has never been a problem before but for some reason it's now screwing everything up.

    What changed? As in, what code / environment / other variables has changed since it worked before? Any little thing ...

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Nothing that I can think of has changed. THe code is the same, it's still running on the same server as the others and the variable %db was changed to %default. That's why I'm confused as to why it's doing this.

      Only one person will use this so I don't have to worry about locking the file.

      Thanks