Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

I have seen time and time again "don't use dbmopen because it is deprecated". I have seen people preface their posts with "I know dbmopen is deprecated and I should use tie, but this isn't my code and I need it fixed quickly", as though it was something to hide. I have seen responses to questions to "read the documentation on tie", but no concrete examples of how to do it.

Well, I am new to Perl and this was a little hard for me to swallow. I mean, it couldn't be that hard - right? Maybe - I only found one good example from a super search here, but it used DB_File, which isn't part of the core distribution. Through google searches, Perl docs, and some trial and error, I was able to come up with the following.

#!/usr/bin/perl -w use strict; use AnyDBM_File; use Fcntl; tie (my %hash, "AnyDBM_File", "filename", O_CREAT|O_RDWR, 0600) or die + "Can't open database: $!\n"; # insert code for %hash here untie %hash;

BTW - The super search results came after the trial and error since it was the exact syntax that I got a hit on.

I verified this both on ActivePerl and on a HPUX installation. Is this the most acceptable way of doing this, or is there a better way? I said maybe earlier, because it is entirely possible that I just missed something obvious.

Thanks in advance,

Limbic~Region

Replies are listed 'Best First'.
Re: To tie or to dbmopen
by PodMaster (Abbot) on Sep 08, 2002 at 10:04 UTC
    So the question is "To tie or dbmopen?" I say to tie. Why, it's simple, you can only dbmopen hashes, but you can tie damn near anything (arrays, scalars, filehandles ...).

    perldoc -f tie reveals everything you need to know (don't know what you're talking about)

        tie VARIABLE,CLASSNAME,LIST
                This function binds a variable to a package class that will
                provide the implementation for the variable. VARIABLE is the
                name of the variable to be enchanted. CLASSNAME is the name of a
                class implementing objects of correct type. Any additional
                arguments are passed to the "new" method of the class (meaning
                "TIESCALAR", "TIEHANDLE", "TIEARRAY", or "TIEHASH"). Typically
                these are arguments such as might be passed to the "dbm_open()"
                function of C. The object returned by the "new" method is also
                returned by the "tie" function, which would be useful if you
                want to access other methods in CLASSNAME.
    
                Note that functions such as "keys" and "values" may return huge
                lists when used on large objects, like DBM files. You may prefer
                to use the "each" function to iterate over such. Example:
    
                    # print out history file offsets
                    use NDBM_File;
                    tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
                    while (($key,$val) = each %HIST) {
                        print $key, ' = ', unpack('L',$val), "\n";
                    }
                    untie(%HIST);
    
                A class implementing a hash should have the following methods:
    
                    TIEHASH classname, LIST
                    FETCH this, key
                    STORE this, key, value
                    DELETE this, key
                    CLEAR this
                    EXISTS this, key
                    FIRSTKEY this
                    NEXTKEY this, lastkey
                    DESTROY this
                    UNTIE this
    
                A class implementing an ordinary array should have the following
                methods:
    
                    TIEARRAY classname, LIST
                    FETCH this, key
                    STORE this, key, value
                    FETCHSIZE this
                    STORESIZE this, count
                    CLEAR this
                    PUSH this, LIST
                    POP this
                    SHIFT this
                    UNSHIFT this, LIST
                    SPLICE this, offset, length, LIST
                    EXTEND this, count
                    DESTROY this
                    UNTIE this
    
                A class implementing a file handle should have the following
                methods:
    
                    TIEHANDLE classname, LIST
                    READ this, scalar, length, offset
                    READLINE this
                    GETC this
                    WRITE this, scalar, length, offset
                    PRINT this, LIST
                    PRINTF this, format, LIST
                    BINMODE this
                    EOF this
                    FILENO this
                    SEEK this, position, whence
                    TELL this
                    OPEN this, mode, LIST
                    CLOSE this
                    DESTROY this
                    UNTIE this
    
                A class implementing a scalar should have the following methods:
    
                    TIESCALAR classname, LIST
                    FETCH this,
                    STORE this, value
                    DESTROY this
                    UNTIE this
    
                Not all methods indicated above need be implemented. See the
                perltie manpage, the Tie::Hash manpage, the Tie::Array manpage,
                the Tie::Scalar manpage, and the Tie::Handle manpage.
    
                Unlike "dbmopen", the "tie" function will not use or require a
                module for you--you need to do that explicitly yourself. See the
                DB_File manpage or the Config module for interesting "tie"
                implementations.
    
                For further details see the perltie manpage, the section on
                "tied VARIABLE".
    

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: To tie or to dbmopen
by Nemp (Pilgrim) on Sep 08, 2002 at 15:10 UTC
    Also being new to Perl I'd like to ask what people mean when they say dbmopen is deprecated? I'd always taken the term deprecated to mean "don't use this feature anymore because there are other ways of doing it and it may not appear in subsequent versions"

    However, in the Llama book merlyn (who's input to this question would be greatly appreciated if at all possible :)) and Tom Phoenix specifically use language that says (amongst other things);

    "other documentation .. claims that dbmopen is deprecated"

    Does this mean dbmopen isn't really deprecated, or is nobody really sure? Is there some place where there is a list of language features that are deprecated? In the spirit of TMTOWTDI should we still be able to use dbmopen when just tieing to hashes? (Especially as the Llama book teaches that as a simple way to do it... and remembering that Perl lets us make the easy things easy?)

    Thanks for your time and patience,
    Neil
      I believe I remember reading that dbmopen is emulated by doing a behind-your-back tie on Perl 5. Which would mean that it is indeed deprecated. However, I can't seem to find anything definitive in the docs at the moment, so don't quote me on this. If anyone could confirm or disprove this, I'd be glad to know for sure.

      Makeshifts last the longest.

Re: To tie or to dbmopen
by Anonymous Monk on Sep 08, 2002 at 15:01 UTC
    It was almost 4AM when I posted that, and I did make a mistake.

    Maybe - I only found one good example from a super search here, but it used DB_File, which isn't part of the core distribution.

    should read

    Maybe - I only found one good example from a super search here and many references to DB_File, but they both required modules which are not part of the core distribution. Well, at least NDBM_File was not on my installation of ActivePerl

    Obviously tie is the better choice or dbmopen wouldn't have become deprecated, but why was it so hard to find an example of how to do it.

    Limbic~Region