14.2. Emptying a DBM FileProblemYou want to clear out a DBM file. Solution
Open the database and assign dbmopen(%HASH, $FILENAME, 0666) or die "Can't open FILENAME: $!\n"; %HASH = (); dbmclose %HASH;
or use DB_File; tie(%HASH, "DB_File", $FILENAME) or die "Can't open FILENAME: $!\n"; %HASH = (); untie %hash; Alternatively, delete the file and reopen with create mode: unlink $FILENAME or die "Couldn't unlink $FILENAME to empty the database: $!\n"; dbmopen(%HASH, $FILENAME, 0666) or die "Couldn't create $FILENAME database: $!\n"; DiscussionIt may be quicker to delete the file and create a new one than to reset it, but doing so opens you up to a race condition that trips up a careless program or makes it vulnerable to an attacker. The attacker could make a link pointing to the file /etc/precious with the same name as your file between the time when you deleted the file and when you recreated it. When the DBM library opens the file, it clobbers /etc/precious . If you delete a DB_File database and recreate it, you'll lose any customizable settings like page size, fill-factor, and so on. This is another good reason to assign the empty list to the tied hash. See Also
The documentation for the standard DB_File module, also in
Chapter 7
of
Programming Perl
; the Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|