This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make MULTICALL handle list context
[perl5.git] / ext / SDBM_File / SDBM_File.pm
1 package SDBM_File;
2
3 use strict;
4 use warnings;
5
6 require Tie::Hash;
7 require XSLoader;
8
9 our @ISA = qw(Tie::Hash);
10 our $VERSION = "1.13";
11
12 our @EXPORT_OK = qw(PAGFEXT DIRFEXT PAIRMAX);
13 use Exporter "import";
14
15 XSLoader::load();
16
17 1;
18
19 __END__
20
21 =head1 NAME
22
23 SDBM_File - Tied access to sdbm files
24
25 =head1 SYNOPSIS
26
27  use Fcntl;   # For O_RDWR, O_CREAT, etc.
28  use SDBM_File;
29
30  tie(%h, 'SDBM_File', 'filename', O_RDWR|O_CREAT, 0666)
31    or die "Couldn't tie SDBM file 'filename': $!; aborting";
32
33  # Now read and change the hash
34  $h{newkey} = newvalue;
35  print $h{oldkey}; 
36  ...
37
38  untie %h;
39
40 =head1 DESCRIPTION
41
42 C<SDBM_File> establishes a connection between a Perl hash variable and
43 a file in SDBM_File format.  You can manipulate the data in the file
44 just as if it were in a Perl hash, but when your program exits, the
45 data will remain in the file, to be used the next time your program
46 runs.
47
48 =head2 Tie
49
50 Use C<SDBM_File> with the Perl built-in C<tie> function to establish
51 the connection between the variable and the file.
52
53     tie %hash, 'SDBM_File', $basename, $modeflags, $perms;
54
55     tie %hash, 'SDBM_File', $dirfile,  $modeflags, $perms, $pagfilename;
56
57 C<$basename> is the base filename for the database.  The database is two
58 files with ".dir" and ".pag" extensions appended to C<$basename>,
59
60     $basename.dir     (or .sdbm_dir on VMS, per DIRFEXT constant)
61     $basename.pag
62
63 The two filenames can also be given separately in full as C<$dirfile>
64 and C<$pagfilename>.  This suits for two files without ".dir" and ".pag"
65 extensions, perhaps for example two files from L<File::Temp>.
66
67 C<$modeflags> can be the following constants from the C<Fcntl> module (in
68 the style of the L<open(2)> system call),
69
70     O_RDONLY          read-only access
71     O_WRONLY          write-only access
72     O_RDWR            read and write access
73
74 If you want to create the file if it does not already exist then bitwise-OR
75 (C<|>) C<O_CREAT> too.  If you omit C<O_CREAT> and the database does not
76 already exist then the C<tie> call will fail.
77
78     O_CREAT           create database if doesn't already exist
79
80 C<$perms> is the file permissions bits to use if new database files are
81 created.  This parameter is mandatory even when not creating a new database.
82 The permissions will be reduced by the user's umask so the usual value here
83 would be 0666, or if some very private data then 0600.  (See
84 L<perlfunc/umask>.)
85
86 =head1 EXPORTS
87
88 SDBM_File optionally exports the following constants:
89
90 =over
91
92 =item *
93
94 C<PAGFEXT> - the extension used for the page file, usually C<.pag>.
95
96 =item *
97
98 C<DIRFEXT> - the extension used for the directory file, C<.dir>
99 everywhere but VMS, where it is C<.sdbm_dir>.
100
101 =item *
102
103 C<PAIRMAX> - the maximum size of a stored hash entry, including the
104 length of both the key and value.
105
106 =back
107
108 These constants can also be used with fully qualified names,
109 eg. C<SDBM_File::PAGFEXT>.
110
111 =head1 DIAGNOSTICS
112
113 On failure, the C<tie> call returns an undefined value and probably
114 sets C<$!> to contain the reason the file could not be tied.
115
116 =head2 C<sdbm store returned -1, errno 22, key "..." at ...>
117
118 This warning is emitted when you try to store a key or a value that
119 is too long.  It means that the change was not recorded in the
120 database.  See BUGS AND WARNINGS below.
121
122 =head1 BUGS AND WARNINGS
123
124 There are a number of limits on the size of the data that you can
125 store in the SDBM file.  The most important is that the length of a
126 key, plus the length of its associated value, may not exceed 1008
127 bytes.
128
129 See L<perlfunc/tie>, L<perldbmfilter>, L<Fcntl>
130
131 =cut