dbmclose#
Break the binding between a DBM file and a hash previously attached by
dbmopen.
dbmclose is the teardown counterpart to dbmopen. It
flushes any pending writes, releases the DBM library’s handles, and
leaves HASH as an ordinary, untied hash you can continue to use or
discard. In modern code dbmclose has been largely superseded by
untie — the two accomplish the same thing, but untie
works for every tied variable, not just DBM-bound hashes. Prefer
untie unless you are maintaining existing dbmopen/dbmclose
pairs and want them to read symmetrically.
Synopsis#
dbmclose HASH
dbmclose %hash
What you get back#
True on success, false on failure. Failure modes are rare — the common
one is passing a hash that was never tied to a DBM file in the first
place, in which case dbmclose returns false and sets $!.
Most programs do not check the return value; the interesting errors
(disk full, quota exceeded) surface on the writes that preceded the
close, not on dbmclose itself. Check it when the DBM file is on a
filesystem where a delayed write failure at close time is plausible:
dbmclose(%cache)
or warn "dbmclose failed: $!";
Global state it touches#
dbmclose reads and mutates the tie state of HASH. It does not
touch any of the usual I/O-related special variables
($,, $\, $|, the
selected filehandle); DBM access does not flow through the PerlIO
layers that print and friends use. On failure, $! is
set by the underlying DBM library.
Examples#
Open a DBM file, iterate it, close it:
dbmopen(%HIST, '/usr/lib/news/history', 0666)
or die "dbmopen: $!";
while (my ($key, $val) = each %HIST) {
print $key, ' = ', unpack('L', $val), "\n";
}
dbmclose(%HIST);
Equivalent using the modern tie/untie pair — this is
what new code should look like:
use AnyDBM_File;
use Fcntl;
tie(my %HIST, 'AnyDBM_File', '/usr/lib/news/history', O_RDWR, 0666)
or die "tie: $!";
# ... use %HIST ...
untie %HIST;
Guard against closing an unbound hash:
my %h;
dbmclose(%h)
or warn "not tied to a DBM file: $!"; # warns
Pair with an eval block to localise any cleanup errors without
aborting the surrounding code:
eval {
dbmclose(%cache) or die "dbmclose: $!";
1;
} or warn $@;
Edge cases#
Hash not tied to a DBM file: returns false, sets
$!. No exception is raised. The hash is left unchanged.Hash tied via
tie, notdbmopen:dbmclosestill works and is equivalent tountiefor that hash, becausedbmopenis itself implemented as atieunder the hood. Mixing the two APIs is legal but confusing — pick one per hash and stay with it.References still held to the tied object: like
untie,dbmclosesevers the binding between the hash and the DBM file, but if other code still holds a reference to the underlying tied object the DBM library may not actually release its handles until that reference is dropped. The symptom is a DBM file that stays locked or open past thedbmclosecall. Ause warningsprogram receives theuntie attempted while N inner references still existwarning in that case.Calling
dbmclosetwice on the same hash: the first call succeeds, the second returns false. Idempotent-style code should either track whether the hash is currently tied or ignore the failure.Hash is
local’d or goes out of scope withoutdbmclose: the binding is broken implicitly when the hash is destroyed, and the DBM library closes the file as part of that teardown. Explicitdbmcloseis preferable because it makes write-flush failures visible at a known point in the program.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
dbmopen— the opening half of the pair; everydbmclosematches a priordbmopenon the same hashuntie— the modern, general-purpose replacement; works for every tied variable, not just DBM-bound hashestie— the modern counterpart todbmopen, used withAnyDBM_File,DB_File,GDBM_File,NDBM_File, orSDBM_Fileto bind a hash to a DBM fileclose— unrelated, but the analogous operation for filehandles: needed when you care about write-flush failures surfacing at a known point