1SDBM_File(3pm) Perl Programmers Reference Guide SDBM_File(3pm)
2
3
4
6 SDBM_File - Tied access to sdbm files
7
9 use Fcntl; # For O_RDWR, O_CREAT, etc.
10 use SDBM_File;
11
12 tie(%h, 'SDBM_File', 'filename', O_RDWR|O_CREAT, 0666)
13 or die "Couldn't tie SDBM file 'filename': $!; aborting";
14
15 # Now read and change the hash
16 $h{newkey} = newvalue;
17 print $h{oldkey};
18 ...
19
20 untie %h;
21
23 "SDBM_File" establishes a connection between a Perl hash variable and a
24 file in SDBM_File format. You can manipulate the data in the file just
25 as if it were in a Perl hash, but when your program exits, the data
26 will remain in the file, to be used the next time your program runs.
27
28 Tie
29 Use "SDBM_File" with the Perl built-in "tie" function to establish the
30 connection between the variable and the file.
31
32 tie %hash, 'SDBM_File', $basename, $modeflags, $perms;
33
34 tie %hash, 'SDBM_File', $dirfile, $modeflags, $perms, $pagfilename;
35
36 $basename is the base filename for the database. The database is two
37 files with ".dir" and ".pag" extensions appended to $basename,
38
39 $basename.dir (or .sdbm_dir on VMS, per DIRFEXT constant)
40 $basename.pag
41
42 The two filenames can also be given separately in full as $dirfile and
43 $pagfilename. This suits for two files without ".dir" and ".pag"
44 extensions, perhaps for example two files from File::Temp.
45
46 $modeflags can be the following constants from the "Fcntl" module (in
47 the style of the open(2) system call),
48
49 O_RDONLY read-only access
50 O_WRONLY write-only access
51 O_RDWR read and write access
52
53 If you want to create the file if it does not already exist then
54 bitwise-OR ("|") "O_CREAT" too. If you omit "O_CREAT" and the database
55 does not already exist then the "tie" call will fail.
56
57 O_CREAT create database if doesn't already exist
58
59 $perms is the file permissions bits to use if new database files are
60 created. This parameter is mandatory even when not creating a new
61 database. The permissions will be reduced by the user's umask so the
62 usual value here would be 0666, or if some very private data then 0600.
63 (See "umask" in perlfunc.)
64
66 SDBM_File optionally exports the following constants:
67
68 · "PAGFEXT" - the extension used for the page file, usually ".pag".
69
70 · "DIRFEXT" - the extension used for the directory file, ".dir"
71 everywhere but VMS, where it is ".sdbm_dir".
72
73 · "PAIRMAX" - the maximum size of a stored hash entry, including the
74 length of both the key and value.
75
76 These constants can also be used with fully qualified names, eg.
77 "SDBM_File::PAGFEXT".
78
80 On failure, the "tie" call returns an undefined value and probably sets
81 $! to contain the reason the file could not be tied.
82
83 "sdbm store returned -1, errno 22, key "..." at ..."
84 This warning is emitted when you try to store a key or a value that is
85 too long. It means that the change was not recorded in the database.
86 See BUGS AND WARNINGS below.
87
89 Do not accept SDBM files from untrusted sources!
90
91 The sdbm file format was designed for speed and convenience, not for
92 portability or security. A maliciously crafted file might cause perl
93 to crash or even expose a security vulnerability.
94
96 There are a number of limits on the size of the data that you can store
97 in the SDBM file. The most important is that the length of a key, plus
98 the length of its associated value, may not exceed 1008 bytes.
99
100 See "tie" in perlfunc, perldbmfilter, Fcntl
101
102
103
104perl v5.30.2 2020-03-27 SDBM_File(3pm)