legLess has asked for the wisdom of the Perl Monks concerning the following question:
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:
The identical code works beautifully if I do either of (while changing $dbh to $DBH, or vice versa ;):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> }
/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 | |
|
Re: Passing DBH into sub
by buck_mulligan (Novice) on Jul 14, 2001 at 04:38 UTC | |
by legLess (Hermit) on Jul 14, 2001 at 04:44 UTC | |
by lestrrat (Deacon) on Jul 14, 2001 at 05:03 UTC | |
|
Re: Passing DBH into sub
by legLess (Hermit) on Jul 14, 2001 at 08:26 UTC |