Problems setting value in MLDBM created database

Discussion in 'Perl Misc' started by Eric, May 10, 2007.

  1. Eric

    Eric Guest

    We have an existing database named 'DBfile' that was created using
    MLDBM. I want to modify the value of 'Cnt' in this database using the
    following script:

    =============================================
    #! /usr/bin/perl

    use Data::Dumper;
    use Fcntl;
    use MLDBM qw(DB_File Storable);

    &DBprint("nfs", "32039", '0');

    sub DBprint {
    my $protocol = shift;
    my $bldNum = shift;
    my $mntPnt = shift;
    my $DBFile = "./DBfile";

    tie( %$mntsDB, "MLDBM", $DBFile, O_CREAT|O_RDWR, 0666,
    $DB_File::DB_BTREE );

    $mntPnt = "mnt".$mntPnt;

    print STDOUT "\nBEFORE\n";

    print STDOUT (Dumper $mntsDB);

    print STDOUT "\nInitial Cnt is: $mntsDB->{$mntPnt}->{Cnt};\n";

    my $tmp = $mntsDB->{$mntPnt}->{Cnt};

    print STDOUT "\n\$tmp is: $tmp\n";

    $tmp{Cnt}[0] = 'foo'; ## key line of code

    print STDOUT "\n\$tmp is now: $tmp->{Cnt}[0]\n";

    $mntsDB->{$mntPnt}->{Cnt} = $tmp;

    print STDOUT "\nCnt is now: $mntsDB->{$mntPnt}->{Cnt};\n";

    print STDOUT "\nAFTER\n";

    print STDOUT (Dumper $mntsDB);

    untie(%$mntsDB);
    }
    =============================================

    But note in the output below that the value is not modified:

    =============================================
    BEFORE
    $VAR1 = {
    'mnt0' => {
    'Cnt' => 2,
    'BldNum' => '32039',
    'Protocol' => 'nfs'
    }
    };

    Initial Cnt is: 2;

    $tmp is: 2

    $tmp is now: foo

    Cnt is now: 2;

    AFTER
    $VAR1 = {
    'mnt0' => {
    'Cnt' => 2,
    'BldNum' => '32039',
    'Protocol' => 'nfs'
    }
    };
    =============================================

    I read the CPAN page carefully, and although it seems as if this
    should work, it does not. The Cnt value is not getting reset to the
    value 'foo'.

    Does anyone see where I am making my mistake? I noted a key line of
    code in the script that I think may be preventing this from working,
    although I did try several variations. I'm thinking that maybe I am
    getting tangled up in my references.

    Thanks.

    Eric
     
    Eric, May 10, 2007
    #1
    1. Advertising

  2. Eric

    Guest

    Eric <> wrote:
    > We have an existing database named 'DBfile' that was created using
    > MLDBM. I want to modify the value of 'Cnt' in this database using the
    > following script:


    <snip code that doesn't compile under strict>

    >
    > Does anyone see where I am making my mistake?


    Yes. From a programming perspective, you apparently aren't using strict.
    From a clpm perspective, you posted code that doesn't compile under
    "use strict". Both of those are mistakes in themselves, and are
    probably hiding other mistakes from you.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 10, 2007
    #2
    1. Advertising

  3. Eric

    Mumia W. Guest

    On 05/10/2007 02:40 PM, Eric wrote:
    > We have an existing database named 'DBfile' that was created using
    > MLDBM. I want to modify the value of 'Cnt' in this database using the
    > following script:
    > [...]


    Here is your program modified to work. I've placed a few numbered
    comments in the code:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use Data::Dumper;
    use Fcntl;
    use DB_File;
    # 1: use DB_File to ensure that DB_BTREE is defined.

    use MLDBM qw(DB_File Storable);

    &DBprint("nfs", "32039", '0');

    sub DBprint {
    my $protocol = shift;
    my $bldNum = shift;
    my $mntPnt = shift;
    my $DBFile = "./DBfile";
    my $mntsDB;

    tie( %$mntsDB, "MLDBM", $DBFile, O_RDWR, 0666, $DB_File::DB_BTREE )
    or die("Tie failure: $!");
    # 2: die() if the tie fails.

    $mntPnt = "mnt".$mntPnt;

    print STDOUT "\nBEFORE\n";

    print STDOUT (Dumper $mntsDB);

    print STDOUT "\nInitial Cnt is: $mntsDB->{$mntPnt}->{Cnt};\n";

    my $tmp = $mntsDB->{$mntPnt};
    # 3: $mntsDB->{$mntPnt}->{Cnt} is a scalar element
    # of a hash--not an array.

    print STDOUT "\n\$tmp is: $tmp\n";

    $tmp->{Cnt} = 'foo'; ## (modified) key line of code

    print STDOUT "\n\$tmp is now: $tmp->{Cnt}\n";

    # $mntsDB->{$mntPnt}->{Cnt} = $tmp;
    $mntsDB->{$mntPnt} = $tmp;
    # 4: Aggregates in a DB_File need to be updated all at once.

    print STDOUT "\nCnt is now: $mntsDB->{$mntPnt}->{Cnt};\n";

    print STDOUT "\nAFTER\n";

    print STDOUT (Dumper $mntsDB);

    untie(%$mntsDB);
    }

    __END__

    Comment #4 is the most important. I can't remember where I first read
    about it, but you can't update a sub-hash within a tied database hash.
    The top-level entry must be updated.
     
    Mumia W., May 11, 2007
    #3
  4. Eric

    Eric Guest

    > Mumia W. wrote:
    <removed>

    Thank you very much for your solution, Munia. I tried it and it does
    in fact work.

    > Comment #4 is the most important. I can't remember where I first read
    > about it, but you can't update a sub-hash within a tied database hash.
    > The top-level entry must be updated.


    Obviously I had made several mistakes that prevented this from
    working, but this one was key.

    Eric
     
    Eric, May 11, 2007
    #4
    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. Jeff Mott

    MLDBM

    Jeff Mott, Aug 12, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    91
    simonis
    Aug 13, 2003
  2. Brian Greenfield

    HoH and MLDBM problems

    Brian Greenfield, Aug 26, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    140
    Bob Walton
    Sep 17, 2003
  3. JS

    mldbm error

    JS, Oct 31, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    215
    Ben Morrow
    Oct 31, 2003
  4. Mark Ng

    MLDBM error

    Mark Ng, Nov 30, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    97
    Mark Ng
    Nov 30, 2004
  5. Replies:
    0
    Views:
    122
Loading...

Share This Page