Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Problem with tie (hash) and dbi?

by smferris (Beadle)
on May 29, 2003 at 21:56 UTC ( [id://261699]=perlquestion: print w/replies, xml ) Need Help??

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

Ok.. Im trying to build a configuration module that will suck the options from a database, and let you overide them via command line arguments

I have been fighting this code for days now and have seemingly found my culprit.. The error I am receiving is:

Can't use an undefined value as a HASH reference at ./x line 46.

In the code sample to follow, if I skip the DBI->connect, the script behaves as it should. (Uncomment the 'last;' in the SKIP block) The code that I'm actually using is attaching to Oracle, below I use the ExampleP driver.. Both exhibit the same problem so I believe it's DBI.. or I'm missing something altogether!

Is there something I'm missing? I just upgraded DBI to 1.37.. originally I was using 1.21. And as noted from the shebang, I'm using a, well, more recent version of perl.. I've also tried 5.005.. same results.

As always, any and all help would be greatly appreciated!
Shawn 8)

And now for some code to demonstrate! (I know it's an incomplete interface.. could that be the problem and I just need to finish it?)

#!/db01/app/perl/5.6.1/bin/perl package Test; use strict; use Data::Dumper; use DBI; sub TIEHASH { bless {}, shift } sub FETCH { my($s,$k,$v)=@_; my $store=$s->{STORE}->{$k} ||= {}; $store->{bar}='baz'; SKIP: { # last; my $dbh=DBI->connect('dbi:ExampleP:','','') or die; } $store; } 1; package main; use Data::Dumper; my %h; tie %h, 'Test'; %h->{a}->{'foo'}='bar'; print Dumper %h->{a};

Thanks again.. Your help is appreciated!

Replies are listed 'Best First'.
Re: Problem with tie (hash) and dbi?
by sauoq (Abbot) on May 29, 2003 at 22:16 UTC
    Can't use an undefined value as a HASH reference at ./x line 46.

    I suspect you didn't get that error from this example as there aren't even 46 lines here. In any case, I'm not sure what you are trying to do, really. This might be your problem though:

    %h->{a}->{'foo'}='bar'; print Dumper %h->{a};
    You are trying to use %h as a hash reference. You probably meant:
    $h{a}->{'foo'} = 'bar'; print Dumper $h{a};

    -sauoq
    "My two cents aren't worth a dime.";
    

      Ooops.. Yeah.. I further narrowed down the code by removing the STORE method as it's not used in this particular scenario.. so the line number was wrong.. However, I believe the syntax %h->{a} is (from what I can make of it) equivelent to $h{a}.. IMO, It's just syntactically clearer.

      The results are still the same.. $h{a} vs. %h->{a}

      I realize now that I left out some detail.. Doh.. This hash will be tied to rows in a single table.. where the key is a set rows, and the value is a hashref of the key/value pairs. EG: the table structure looks like:

      class varchar,
      key varchar,
      value varchar

      Thus, $h{class}->{key}='value'

      The tied hash is readonly.. there's another mechanism to update them permenantly.. this object will only be able to change them temporarily via command line arguments.

      Hope that helps explain my intentions. Thanks for the response

      SMF 8)

        However, I believe the syntax %h->{a} is (from what I can make of it) equivelent to $h{a}.. IMO, It's just syntactically clearer.

        I'll grant you that it works, but you'll be hard-pressed to find many experienced Perl coders to agree with your assessment that it is "syntactically clearer." It... well... it just isn't. Besides that, it's deprecated in 5.8 so you would do yourself a favor by starting to avoid it now.

        $ perl -Mstrict -wle 'my %h=qw(foo bar); print %h->{foo}' Using a hash as a reference is deprecated at -e line 1. bar

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Problem with tie (hash) and dbi?
by Itatsumaki (Friar) on May 29, 2003 at 23:05 UTC
    There is nothing in general that prevents DBI and tie from working together. As someone else noted, are you sure you want %h->{a} not $h->{a}? Also, do you get the error if you remove the connect from the sub and allow yourself to have a global dbh?
    -Tats
Re: Problem with tie (hash) and dbi?
by smferris (Beadle) on Jun 06, 2003 at 18:00 UTC

    Ok.. this just keeps getting more weird.. I know I'm missing something.. Given, mostly the same code, slightly commented:

    #!/db01/app/perl/5.6.1/bin/perl package Test; use strict; use Data::Dumper; use DBI; sub TIEHASH { my($class)=@_; my($self)=bless {}, $class; # POSITION 1 $self; } sub FETCH { my($s,$k,$v)=@_; # POSITION 2 my $store=$s->{STORE}->{$k} ||= {}; $store->{bar}='baz'; # POSITION 3 my $dbh=DBI->connect('dbi:ExampleP:','','') or die; $store; } 1; package main; use Data::Dumper; my %h; tie %h, 'Test'; $h{a}->{'foo'}='bar'; print Dumper $h{a};

    Leaving the connect at position 3 (see the comments) it doesn't work at all.. it errors as mentioned previously.. Move the connect to position 2.. no error.. but foo isn't set either.. however, the data from the query (missing in the above code) does get populated into the hash properly.. If I move the connect to position 1.. it works exactly as I intended it. Wha.. huh!? foo is set, the data from the query populates properly.. and the Dumper in main is exactly as I expect..

    What don't I see? I'm guessing some sort of scope issue.. ??

    Again, any help.. GREATLY appreciated!
    SMF 8)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://261699]
Approved by sauoq
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-05-28 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found