Friday, October 10, 2008

How to pass an object in a function

It is really simple concept but can confuse novice to perl. An object can be passed to a function just like an ordinary variable. File handler need special syntax to be
passed inside a function but not an object.

Follow this simple example here. Code is incomplete but is tested when complete.

Here it goes:

#!/opt/local/bin/perl

use lib "$ENV{HOME}/perl/lib";
use convert_time;
use dir_pool;
use db_connect;
use warnings;
use strict;

my $obj = db_connect->new();

&doConnectMIR($obj);
print " \n";
&doConnectCIR($obj);


sub doConnectMIR($)
{
my $tObj = shift;

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

$tObj = db_connect->new();
$dbh = $obj->connect_qora007_mir("qora007_mir");

$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";
}


more code follows.....but concept is clear.....