perlfunc
gods
<P>
dbmopen - create binding on a tied dbm file
<P>
<HR>
<P>
dbmopen
<FONT SIZE=-1>HASH,DBNAME,MODE</FONT>
<P>
<HR>
<P>
[This function has been superseded by the [perlfunc:tie|tie()] function.]
<P>
This binds a
<CODE>dbm(3),</CODE>
<CODE>ndbm(3),</CODE>
<CODE>sdbm(3),</CODE>
<CODE>gdbm(3),</CODE> or Berkeley
<FONT SIZE=-1>DB</FONT> file to a hash.
<FONT SIZE=-1>HASH</FONT> is the name of the hash. (Unlike normal
[perlfunc:open|open()], the first argument is <EM>NOT</EM> a filehandle, even though it looks like one).
<FONT SIZE=-1>DBNAME</FONT> is the name of the database (without the <EM>.dir</EM> or <EM>.pag</EM> extension if any). If the database does not exist, it is created with protection specified by
<FONT SIZE=-1>MODE</FONT> (as modified by the
[perlfunc:umask|umask()]). If your system supports only the older
<FONT SIZE=-1>DBM</FONT> functions, you may perform only one [perlfunc:dbmopen|dbmopen()] in your program. In older versions of Perl, if your system had neither
<FONT SIZE=-1>DBM</FONT> nor ndbm, calling [perlfunc:dbmopen|dbmopen()] produced a fatal error; it now falls back to
<CODE>sdbm(3).</CODE>
<P>
If you don't have write access to the
<FONT SIZE=-1>DBM</FONT> 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 an [perlfunc:eval|eval()], which will trap the error.
<P>
Note that functions such as [perlfunc:keys|keys()] and [perlfunc:values|values()] may return huge lists when used on large
<FONT SIZE=-1>DBM</FONT> files. You may prefer to use the [perlfunc:each|each()]
function to iterate over large
<FONT SIZE=-1>DBM</FONT> files. Example:
<P>
<PRE> # 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);
</PRE>
<P>
See also [perlfunc:../../lib/AnyDBM_File|the AnyDBM_File manpage] for a more general description of the pros and cons of the various dbm
approaches, as well as [perlfunc:../../lib/DB_File|the DB_File manpage] for a particularly rich implementation.
<HR>