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.
In reply to Passing DBH into sub by legLess
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |