Recipe 14.2 Emptying a DBM File
use DB_File; tie(%HASH, "DB_File", $FILENAME) or die "Can't open FILENAME: $!\n"; %HASH = ( ); untie %HASH;
Alternatively, delete the file and reopen:
unlink $FILENAME or die "Couldn't unlink $FILENAME to empty the database: $!\n"; tie(%HASH => "DB_File", $FILENAME) or die "Couldn't create $FILENAME database: $!\n";
It 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.
14.2.4 See Also
The documentation for the standard DB_File module, also in Chapter 32 of Programming Perl; the unlink function in perlfunc(1); Recipe 14.1