HoH and MLDBM problems

Discussion in 'Perl Misc' started by Brian Greenfield, Aug 26, 2003.

  1. I've got a HoH which I'm trying to store using MLDBM and DB_File. The
    following minimal script demonstrates the problem I've got.

    Using an untied hash works ok:

    |zippy:~/scripts$ ./test 0 # untied
    |$VAR1 = {
    | 'fruit' => {
    | 'banana' => 1,
    | 'apple' => 1,
    | 'orange' => 1
    | }
    | };

    But,

    |zippy:~/scripts$ ./test 1 # tied
    |Using tie
    |$VAR1 = {
    | 'fruit' => {}
    | };

    And the script:

    |zippy:~/scripts$ cat test
    |#!/usr/bin/perl
    |
    |use strict;
    |use warnings FATAL=>'all';
    |use Data::Dumper;
    |use MLDBM qw/DB_File/;
    |
    |unlink "my.db";
    |my $use_tie = shift || 0;
    |my %db;
    |
    |if ($use_tie)
    |{
    | print "Using tie\n";
    | tie %db, "MLDBM", 'my.db' or die "can't tie: $!";
    |}
    |
    |$db{fruit}{$_} = 1 for qw/apple orange banana/;
    |print Dumper \%db;
    |untie %db;

    Perl is 5.8.0 running on Debian/testing.
    MLDBM is 2.01
    DB_File is 1.804
    and please don't ask which version of libdb I'm using :(

    Any input appreciated.
     
    Brian Greenfield, Aug 26, 2003
    #1
    1. Advertising

  2. Also sprach Brian Greenfield:

    > I've got a HoH which I'm trying to store using MLDBM and DB_File. The
    > following minimal script demonstrates the problem I've got.


    [...]

    >|zippy:~/scripts$ cat test
    >|#!/usr/bin/perl
    >|
    >|use strict;
    >|use warnings FATAL=>'all';
    >|use Data::Dumper;
    >|use MLDBM qw/DB_File/;
    >|
    >|unlink "my.db";
    >|my $use_tie = shift || 0;
    >|my %db;
    >|
    >|if ($use_tie)
    >|{
    >| print "Using tie\n";
    >| tie %db, "MLDBM", 'my.db' or die "can't tie: $!";
    >|}
    >|
    >|$db{fruit}{$_} = 1 for qw/apple orange banana/;


    You can't store those data like that. When working with a tied hash, you
    can only store something in the top-level and not somewhere nested
    below. The above would have to read:

    my %subhash;
    $subhash{ $_ } = 1 for qw/apple orange banana/;
    $db{ fruit } = \%subhash;

    This is a known limitation of the tied interface. See also the "BUGS"
    section in 'perldoc MLDBM'.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Aug 26, 2003
    #2
    1. Advertising

  3. On 26 Aug 2003 20:52:28 GMT, "Tassilo v. Parseval"
    <> wrote:

    >Also sprach Brian Greenfield:


    [snip]

    >>|if ($use_tie)
    >>|{
    >>| print "Using tie\n";
    >>| tie %db, "MLDBM", 'my.db' or die "can't tie: $!";
    >>|}
    >>|
    >>|$db{fruit}{$_} = 1 for qw/apple orange banana/;

    >
    >You can't store those data like that. When working with a tied hash, you
    >can only store something in the top-level and not somewhere nested
    >below. The above would have to read:
    >
    > my %subhash;
    > $subhash{ $_ } = 1 for qw/apple orange banana/;
    > $db{ fruit } = \%subhash;
    >
    >This is a known limitation of the tied interface. See also the "BUGS"
    >section in 'perldoc MLDBM'.


    I know:(

    I just read the docs *again* (although with a little more care), and
    saw this limitation.

    Thanks for the help and my apologies for wasting your time.
     
    Brian Greenfield, Aug 26, 2003
    #3
  4. Brian Greenfield

    Tom Guest

    I have been trying to get the following to work for weeks and it just does
    not.
    No data gets stored like this as long as it is tied via tie or dbmopen.

    >
    > You can't store those data like that. When working with a tied hash, you
    > can only store something in the top-level and not somewhere nested
    > below. The above would have to read:
    >
    > my %subhash;
    > $subhash{ $_ } = 1 for qw/apple orange banana/;
    > $db{ fruit } = \%subhash;
    >



    Any clues?
    Thanks,
    Tom
     
    Tom, Sep 16, 2003
    #4
  5. Brian Greenfield

    Bob Walton Guest

    Tom wrote:

    > I have been trying to get the following to work for weeks and it just does
    > not.
    > No data gets stored like this as long as it is tied via tie or dbmopen.
    >
    >
    >>You can't store those data like that. When working with a tied hash, you
    >>can only store something in the top-level and not somewhere nested
    >>below. The above would have to read:
    >>
    >> my %subhash;
    >> $subhash{ $_ } = 1 for qw/apple orange banana/;
    >> $db{ fruit } = \%subhash;
    >>
    >>

    >
    >
    > Any clues?



    Yes. Look at the MLDBM module. If the docs are mystifying, read the
    relevant recipies in Perl Cookbook, which has an excellent discussion.
    Recognize that the fundamental problem is that while a "regular" hash
    (not tied to a DBM-type file) can store any scalar value including a
    reference, a hash which is tied to a DBM-type file can only store one
    kind of thing: a string. MLDBM functions by automagically stringifying
    the item to be stored and unstringifying it when it is fetched. It
    works fine, but you must bear in mind the fact that if you want to
    change something in the stored structure, you must retrieve the entire
    stored structure, make your modification to that, and then store the
    entire structure back again.

    HTH.


    ....
    > Tom


    --
    Bob Walton
     
    Bob Walton, Sep 17, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron DeLoach

    HoHoH (hash of HoH's)

    Aaron DeLoach, Jul 18, 2004, in forum: Perl Misc
    Replies:
    10
    Views:
    175
    Aaron DeLoach
    Jul 20, 2004
  2. Replies:
    1
    Views:
    114
    Gunnar Hjalmarsson
    Jan 17, 2005
  3. Replies:
    0
    Views:
    129
  4. Replies:
    2
    Views:
    103
  5. Eric
    Replies:
    3
    Views:
    102
Loading...

Share This Page