in reply to Perl DBM

Actually exists() should work fine with all dbm files. They function like hashes. Can you give an example of the problem you're having?

Replies are listed 'Best First'.
Re: Re: Perl DBM
by Anonymous Monk on Apr 14, 2004 at 20:08 UTC
    Hi,
    Thanks for the response. The following works w/ the tie-statement commented out (as below):
    2 use SDBM_File; 3 use MLDBM qw (SDBM_File); 4 use Fcntl; 5 6 #tie (%h, 'MLDBM', 'dbm_file', O_CREAT|O_RDWR, 0666) || die "c +ouldnt open tie-file\n"; 7 8 $h{ "1" } = { "a" => "first" }; 9 $h{ "2" } = { "a" => "second" }; 10 $h{ "3" } = { "a" => "third" }; 11 12 foreach ( keys %h ) 13 { 14 $value = ${ $h{ $_ } }{ "a" }; 15 print "key: .$_. value: .$value.\n"; 16 }; 17 18 if ( exists $h{ "1" } ) 19 { 20 $h{ "1" } = [ "a", "b", "c" ]; 21 22 foreach ( @{ $h{ "1" } } ) 23 { 24 print "value is: .$_.\n"; 25 }; 26 };
    I get:
    key: .1. value: .first. key: .2. value: .second. key: .3. value: .third. value is: .a. value is: .b. value is: .c.
    but, if I uncomment the tie, I get:
    key: .1. value: .first. key: .2. value: .second. key: .3. value: .third. SDBM_File doesn't define an EXISTS method at dbm.pl line 18
    Any help appreciated. I'm using MLDBM because, per literature I encountered, regular DBM doesn't permit complex data-structures.
    Thanks again -
    MatthewFrancis
      You could just use defined() in this case, but exists() does work on most dbm implementations. Instead of SDBM_File, try DB_File, GDBM_File, or NDBM_File.