K2KO has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I seek knowledge!

Is there a way to use a pointer to a hash (rather than a hash)in a dbmopen statement? It seems to REALLY want a true hash and I've tried various de-referencing schemes (not my strong suit!) to no avail.

The following fails miserably.

my $SR = Record->new; dbmopen( $SR, $DBFile, 0666 ) or die ("Can't create / open $DBFile $!" +);
Edit by castaway - code tags and title change from "dbmopen"

Replies are listed 'Best First'.
Re: Using a hash pointer with dbmopen
by bart (Canon) on Jan 17, 2004 at 19:37 UTC
    Though I have never used dbmopen (I'd go for tie instead), I think the following is worth trying, at least if $SR is indeed a hash ref like you claim it should be:
    dbmopen( %$SR, $DBFile, 0666 )

    I know for a fact that tieing a hash via a hashref does work, in this way.

      Yes, that worked perfectly. I'm not sure how to read "in English" the %$. Is it: "the hash who's scalar pointer is?" So, would @$ be: The array who's scalar pointer is..."

      I need to do a lot of study on referencing and de-referencing. Its pretty confusing to me. I'm printing out those pages of the PERLDOC right now. Good bathroom reading.

      I didn't realize that dbmopen was considered obsolete. Maybe I've got an old PERL book? I'll move to TIE tonight.

      Lots to learn, so little time....

      Thanks!

        I'm not sure how to read "in English" the %$. Is it: "the hash who's scalar pointer is?"
        More or less, yes, except in Perl we say "reference", not pointer. It's not a pointer, which would be nothing other than a raw address; a "reference", OTOH, is a smart, well, "object", with built-in cleverness.

        So I'd call it "the hash referenced by ...".

        I didn't realize that dbmopen was considered obsolete. Maybe I've got an old PERL book?
        What book is that?!? dbmopen has been considered obsolete since the beginning of Perl5, now almost 10 years ago. You must be looking into a perl4 book... :) BTW the first line in the docs for dbmopen explicitely say it's deprecated: "has been superseded".

        tie offers a general mechanism to do hidden stuff behind the masquerade of a simple variable, while dbmopen does just one thing. Why would we need two entirely different interfaces for doing very similar things? So, as tie is far more flexible than dbmopen, it has been chosen as the preferred route.

Re: Using a hash pointer with dbmopen
by ysth (Canon) on Jan 18, 2004 at 05:11 UTC
    You also need to be aware that tie'ing a hash (or array, scalar, or filehandle) makes it forget about the previous contents, as if you had done %$SR = ();. (Once you untie it the previous contents should be accessible again, but I think this is not well tested and subject to bugs.)

    Update: fix spelling