In perl.git, the branch blead has been updated

<http://perl5.git.perl.org/perl.git/commitdiff/f7ffe0466998b9f23f7921986980173aa5a99a66?hp=d3c0b317ea8dd34a3cbcef6b0fc34778cd13fcce>

- Log -----------------------------------------------------------------
commit f7ffe0466998b9f23f7921986980173aa5a99a66
Merge: d3c0b31 18f6a8a
Author: Tony Cook <t...@develop-help.com>
Date:   Wed Dec 18 09:33:31 2013 +1100

    [perl #114350] access to SDBM constants and explicit filenames

commit 18f6a8aad3d1f7c5884271c98ec97236d527e08f
Author: Kevin Ryde <use...@zip.com.au>
Date:   Mon Dec 16 11:44:11 2013 +1100

    [perl #114350] improved documentation

M       ext/SDBM_File/SDBM_File.pm

commit 5ab2cf16cddbadb6290706352c9e867acbb0009b
Author: Tony Cook <t...@develop-help.com>
Date:   Wed Dec 11 14:37:20 2013 +1100

    [perl #114350] access to sdbm_prep()
    
    This allows the .dir and .pag filenames to be specified explicitly

M       MANIFEST
M       ext/SDBM_File/SDBM_File.pm
M       ext/SDBM_File/SDBM_File.xs
A       ext/SDBM_File/t/prep.t

commit 17b33ba0dfe1b4bd7fa056ccb3beaf6d4e5f7a7a
Author: Tony Cook <t...@develop-help.com>
Date:   Thu Dec 5 15:01:11 2013 +1100

    [perl #114350] add exportable PAGFEXT, DIRFEXT and PAIRMAX constants

M       MANIFEST
M       ext/SDBM_File/SDBM_File.pm
M       ext/SDBM_File/SDBM_File.xs
A       ext/SDBM_File/t/constants.t
-----------------------------------------------------------------------

Summary of changes:
 MANIFEST                    |  2 ++
 ext/SDBM_File/SDBM_File.pm  | 75 +++++++++++++++++++++++++++------------------
 ext/SDBM_File/SDBM_File.xs  | 20 ++++++++++--
 ext/SDBM_File/t/constants.t | 16 ++++++++++
 ext/SDBM_File/t/prep.t      | 34 ++++++++++++++++++++
 5 files changed, 114 insertions(+), 33 deletions(-)
 create mode 100644 ext/SDBM_File/t/constants.t
 create mode 100644 ext/SDBM_File/t/prep.t

diff --git a/MANIFEST b/MANIFEST
index 9e26312..08663b3 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -3787,6 +3787,8 @@ ext/SDBM_File/sdbm/sdbm.c SDBM kit
 ext/SDBM_File/sdbm/sdbm.h      SDBM kit
 ext/SDBM_File/sdbm/tune.h      SDBM kit
 ext/SDBM_File/sdbm/util.c      SDBM kit
+ext/SDBM_File/t/constants.t    See if SDBM_File constants work
+ext/SDBM_File/t/prep.t         See if SDBM_File with extra argument works
 ext/SDBM_File/t/sdbm.t         See if SDBM_File works
 ext/SDBM_File/typemap          SDBM extension interface types
 ext/Sys-Hostname/Hostname.pm   Sys::Hostname extension Perl module
diff --git a/ext/SDBM_File/SDBM_File.pm b/ext/SDBM_File/SDBM_File.pm
index d369b54..dcb12bc 100644
--- a/ext/SDBM_File/SDBM_File.pm
+++ b/ext/SDBM_File/SDBM_File.pm
@@ -7,7 +7,10 @@ require Tie::Hash;
 require XSLoader;
 
 our @ISA = qw(Tie::Hash);
-our $VERSION = "1.10";
+our $VERSION = "1.11";
+
+our @EXPORT_OK = qw(PAGFEXT DIRFEXT PAIRMAX);
+use Exporter "import";
 
 XSLoader::load();
 
@@ -37,62 +40,74 @@ SDBM_File - Tied access to sdbm files
 =head1 DESCRIPTION
 
 C<SDBM_File> establishes a connection between a Perl hash variable and
-a file in SDBM_File format;.  You can manipulate the data in the file
+a file in SDBM_File format.  You can manipulate the data in the file
 just as if it were in a Perl hash, but when your program exits, the
 data will remain in the file, to be used the next time your program
 runs.
 
+=head2 Tie
+
 Use C<SDBM_File> with the Perl built-in C<tie> function to establish
-the connection between the variable and the file.  The arguments to
-C<tie> should be:
+the connection between the variable and the file.
 
-=over 4
+    tie %hash, 'SDBM_File', $basename,    $modeflags, $perms;
 
-=item 1.
+    tie %hash, 'SDBM_File', $dirfilename, $modeflags, $perms, $pagfilename;
 
-The hash variable you want to tie.
+C<$basename> is the base filename for the database.  The database is two
+files with ".dir" and ".pag" extensions appended to C<$basename>,
 
-=item 2. 
+    $basename.dir     (or .sdbm_dir on VMS, per DIRFEXT constant)
+    $basename.pag
 
-The string C<"SDBM_File">.  (Ths tells Perl to use the C<SDBM_File>
-package to perform the functions of the hash.)
+The two filenames can also be given separately in full as C<$dirfilename>
+and C<$pagfilename>.  This suits for two files without ".dir" and ".pag"
+extensions, perhaps for example two files from L<File::Temp>.
 
-=item 3. 
+C<$modeflags> can be the following constants from the C<Fcntl> module (in
+the style of the L<open(2)> system call),
 
-The name of the file you want to tie to the hash.  
+    O_RDONLY          read-only access
+    O_WRONLY          write-only access
+    O_RDWR            read and write access
 
-=item 4.
+If you want to create the file if it does not already exist then bitwise-OR
+(C<|>) C<O_CREAT> too.  If you omit C<O_CREAT> and the database does not
+already exist then the C<tie> call will fail.
 
-Flags.  Use one of:
+    O_CREAT           create database if doesn't already exist
 
-=over 2
+C<$perms> is the file permissions bits to use if new database files are
+created.  This parameter is mandatory even when not creating a new database.
+The permissions will be reduced by the user's umask so the usual value here
+would be 0666, or if some very private data then 0600.  (See
+L<perlfunc/umask>.)
 
-=item C<O_RDONLY>
+=head1 EXPORTS
 
-Read-only access to the data in the file.
+SDBM_File optionally exports the following constants:
 
-=item C<O_WRONLY>
+=over
 
-Write-only access to the data in the file.
+=item *
 
-=item C<O_RDWR>
+C<PAGFEXT> - the extension used for the page file, usually C<.pag>.
 
-Both read and write access.
+=item *
 
-=back
+C<DIRFEXT> - the extension used for the directory file, C<.dir>
+everywhere but VMS, where it is C<.sdbm_dir>.
 
-If you want to create the file if it does not exist, add C<O_CREAT> to
-any of these, as in the example.  If you omit C<O_CREAT> and the file
-does not already exist, the C<tie> call will fail.
+=item *
 
-=item 5.
-
-The default permissions to use if a new file is created.  The actual
-permissions will be modified by the user's umask, so you should
-probably use 0666 here. (See L<perlfunc/umask>.)
+C<PAIRMAX> - the maximum size of a stored hash entry, including the
+length of both the key and value.
 
 =back
 
+These constants can also be used with fully qualified names,
+eg. C<SDBM_File::PAGFEXT>.
+
 =head1 DIAGNOSTICS
 
 On failure, the C<tie> call returns an undefined value and probably
diff --git a/ext/SDBM_File/SDBM_File.xs b/ext/SDBM_File/SDBM_File.xs
index 2da097e..070f074 100644
--- a/ext/SDBM_File/SDBM_File.xs
+++ b/ext/SDBM_File/SDBM_File.xs
@@ -19,7 +19,6 @@ typedef SDBM_File_type * SDBM_File ;
 typedef datum datum_key ;
 typedef datum datum_value ;
 
-#define sdbm_TIEHASH(dbtype,filename,flags,mode) sdbm_open(filename,flags,mode)
 #define sdbm_FETCH(db,key)                     sdbm_fetch(db->dbp,key)
 #define sdbm_STORE(db,key,value,flags)         
sdbm_store(db->dbp,key,value,flags)
 #define sdbm_DELETE(db,key)                    sdbm_delete(db->dbp,key)
@@ -31,17 +30,24 @@ typedef datum datum_value ;
 MODULE = SDBM_File     PACKAGE = SDBM_File     PREFIX = sdbm_
 
 SDBM_File
-sdbm_TIEHASH(dbtype, filename, flags, mode)
+sdbm_TIEHASH(dbtype, filename, flags, mode, pagname=NULL)
        char *          dbtype
        char *          filename
        int             flags
        int             mode
+       char *          pagname
        CODE:
        {
            DBM *       dbp ;
 
            RETVAL = NULL ;
-           if ((dbp = sdbm_open(filename,flags,mode))) {
+           if (pagname == NULL) {
+               dbp = sdbm_open(filename, flags, mode);
+           }
+           else {
+               dbp = sdbm_prep(filename, pagname, flags, mode);
+           }
+           if (dbp) {
                RETVAL = (SDBM_File)safecalloc(1, sizeof(SDBM_File_type));
                RETVAL->dbp = dbp ;
            }
@@ -124,3 +130,11 @@ filter_fetch_key(db, code)
        SDBM_File::filter_store_value = store_value
        CODE:
            DBM_setFilter(db->filter[ix], code);
+
+BOOT:
+        {
+            HV *stash = gv_stashpvs("SDBM_File", 1);
+            newCONSTSUB(stash, "PAGFEXT", newSVpvs(PAGFEXT));
+            newCONSTSUB(stash, "DIRFEXT", newSVpvs(DIRFEXT));
+            newCONSTSUB(stash, "PAIRMAX", newSVuv(PAIRMAX));
+        }
diff --git a/ext/SDBM_File/t/constants.t b/ext/SDBM_File/t/constants.t
new file mode 100644
index 0000000..dcb10d9
--- /dev/null
+++ b/ext/SDBM_File/t/constants.t
@@ -0,0 +1,16 @@
+#!./perl
+use strict;
+use Test::More tests => 4;
+
+use SDBM_File;
+
+# has always been .pag
+is(SDBM_File::PAGFEXT, ".pag", "PAGFEXT");
+
+# depends on the platform
+like(SDBM_File::DIRFEXT, qr/^\.(?:sdbm_)?dir$/, "DIRFEXT");
+
+is(SDBM_File::PAIRMAX, 1008, "PAIRMAX");
+
+ok(eval { SDBM_File->import(qw(PAIRMAX PAGFEXT DIRFEXT)); 1 }, "exportable");
+
diff --git a/ext/SDBM_File/t/prep.t b/ext/SDBM_File/t/prep.t
new file mode 100644
index 0000000..a222a64
--- /dev/null
+++ b/ext/SDBM_File/t/prep.t
@@ -0,0 +1,34 @@
+#!./perl
+use strict;
+use Test::More tests => 4;
+
+use SDBM_File;
+use File::Temp 'tempfile';
+use Fcntl;
+
+my ($dirfh, $dirname) = tempfile();
+my ($pagfh, $pagname) = tempfile();
+
+# close so Win32 allows them to be re-opened
+close $dirfh;
+close $pagfh;
+
+{
+    my %h;
+
+    ok(eval { tie %h, "SDBM_File", $dirname, O_CREAT | O_RDWR | O_TRUNC, 0640, 
$pagname; 1 },
+       "create SDBM with explicit filenames")
+      or diag $@;
+    is(keys %h, 0, "should be empty");
+
+    # basic sanity checks, the real storage checks are done by sdbm.t
+    $h{abc} = 1;
+    $h{def} = 1;
+}
+
+{
+    my %h;
+    ok(eval { tie %h, "SDBM_File", $dirname, O_RDWR, 0640, $pagname; 1 },
+       "open SDBM with explicit filenames");
+    is_deeply([ sort keys  %h] , [ qw(abc def) ], "should have two keys");
+}

--
Perl5 Master Repository

Reply via email to