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

God, I just know I'm doing something stupid.

I'm cleaning up a CGI script which accesses a database. My first version had a database_connect sub which was called by various other subs. The problem, of course, was that sometimes the script needed to access the database more than once (mod_perl isn't an option right now).

So my current plan is to have the script open the database handle once at the beginning and then disconnect at the end, passing the database handle to each sub that needs it. That's exactly what I'm doing with the CGI object, and it works nicely.

It doesn't seem to work with the database handle, however. The error is Can't call method "prepare" on an undefined value, and I understand what it means (I think); I just don't see how my code is generating it. Here's the relevent code:

my $Q = CGI->new(); my $DBH = DBI->connect( <connection string> ) or die 'Error connecting: ' . DBI->errstr; $Q = get_member( $Q, $DBH ); <snip> sub get_member { my $q = shift; my $dbh = shift; my $sth = $dbh->prepare( <query string> ); # here's the error <do stuff> }
The identical code works beautifully if I do either of (while changing $dbh to $DBH, or vice versa ;):
  1. I replace the $dbh = shift; with $dbh = database_connect(); (the database_connect code is identical to the connection code above: copied and pasted).
  2. I take the get_member code out into main
Also, I stole this idea from O'Reilly's CGI Programming with Perl (2nd edition, page 252). I've copied their format exactly, although I haven't tried to run their (pages of) code.

/me braces for clue-sticking. What am I doing wrong?

Thanks muchly
--
man with no legs, inc.

Replies are listed 'Best First'.
Re: Passing DBH into sub
by Hero Zzyzzx (Curate) on Jul 14, 2001 at 06:09 UTC

    I do this exact thing, however, I retrieve the variables in my sub thusly:

    test($q,$dbh); sub test { my ($q,$dbh)=@_; #stuff in here }

    I believe this has something to do with () causing $dbh to be evaluated as a scalar (I know I'm probably murdering the terminology here.)

Re: Passing DBH into sub
by buck_mulligan (Novice) on Jul 14, 2001 at 04:38 UTC
    I believe that error means that the query string isn't getting fed to the prepare method correctly... where it says "<query string>" in your code snippet, what does it literally say in your script? Is it a literal string? A variable? I've encountered that error when I accidentally squashed the variable I was using to hold my query string.... I dunno; hope this helps.
      Thanks for the reply, but read my post a little more carefully. The query string works perfectly if I use a different method to get the database handle.

      The error means that the database handle is undefined, not that the query string is munged somehow.
      --
      man with no legs, inc.

        While he may not have hit the answer you wanted, it's kind of impossible to find out what is wrong with your script just by looking at the post, I think. At least, from what I can tell, your problem is not that get_member function or anything... ergo, the problem must be in the rest of the code.

        My suggestion is to use perl -d to step through the suspicious areas...

Re: Passing DBH into sub
by legLess (Hermit) on Jul 14, 2001 at 08:26 UTC
    >God, I just know I'm doing something stupid.

    *cough* I was right ... ;)
    --
    man with no legs, inc.