Monday, November 10, 2008

Passing a DB handler to an user defined function

It is pretty simply. Pass it like an ordinary variable.

Example here:

#!/opt/local/bin/perl

use db_connect;
use warnings;
use strict;


# More efficient than ./test_connection.pl because $0 makes only one DB connection

my $obj = db_connect->new();
my $db = $obj->connect_qora007_mir("qora007_mir");

print " \n";
&checkConnectionMIR_qora007($db);
&checkConnectionCIR_qora007($db);
print " \n";

$db->disconnect();



sub checkConnectionMIR_qora007($)
{
my $dbh = shift;

my $sql;
my $sth;
my $str;
my @row;

$sql = "select sysdate from dual";

$sth = $dbh->prepare($sql);
$sth->execute;

while((@row) = $sth->fetchrow_array())
{
$str = join(" " , map {defined $_ ? $_ : "(null)"} @row);
print "INSTANCE: $str\n";
}

No comments: