[This function has been largely superseded by thetie
function.]
This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or Berkeley DB file to a hash. HASH is the name of the hash. (Unlike normalopen
, the first argument isnot a filehandle, even though it looks like one). DBNAME is the name of the database (without the.dir or.pag extension if any). If the database does not exist, it is created with protection specified by MASK (as modified by theumask
). If your system supports only the older DBM functions, you may perform only onedbmopen
in your program. In older versions of Perl, if your system had neither DBM nor ndbm, callingdbmopen
produced a fatal error; it now falls back to sdbm(3).
If you don't have write access to the DBM file, you can only read hash variables, not set them. If you want to test whether you can write, either use file tests or try setting a dummy hash entry inside aneval
, which will trap the error.
Note that functions such askeys
andvalues
may return huge lists when used on large DBM files. You may prefer to use theeach
function to iterate over large DBM files. Example:
# print out history file offsets dbmopen(%HIST,'/usr/lib/news/history',0666); while (($key,$val) = each %HIST) {print $key, ' = ', unpack('L',$val), "\n"; } dbmclose(%HIST);
See alsoAnyDBM_File for a more general description of the pros and cons of the various dbm approaches, as well asDB_File for a particularly rich implementation.
You can control which DBM library you use by loading that library before you call dbmopen():
use DB_File; dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")or die "Can't open netscape history file: $!";
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.