DBI object in modules

R

Roman Khutkyy

Can anybody help? I built my script using module structure as i read: "...
and use the subroutines from modules as you are using its directly in main
program." So suppose i have the script main.pl and with 'use' i linked some
modules "Module1.pm", "Module1.pm","ModuleN.pm". The subroutines in each of
these modules use the DBI module to connect to database, make transaction,
query etc. Is there a method to create DBI object once in main.pl and use it
in any subroutine from any module. Now i have to create database connection
in each module, and there is no problem to do it because there is just 6
modules, and i can't see now how this technique slows server down, but when
the number of modules will be much more, or inet traffic will be like on
Microsoft servers :) what to do then?
 
C

ctcgag

Roman Khutkyy said:
Can anybody help? I built my script using module structure as i read:
"... and use the subroutines from modules as you are using its directly
in main program." So suppose i have the script main.pl and with 'use' i
linked some modules "Module1.pm", "Module1.pm","ModuleN.pm". The
subroutines in each of these modules use the DBI module to connect to
database, make transaction, query etc. Is there a method to create DBI
object once in main.pl and use it in any subroutine from any module.


You could always make each subroutine in the modules take $dbh as one of
the parameters. It's somewhat annoying to have give the added parameter
each time, but I like this method anyway as it forces you to stare the $dbh
in the face. If you abstract away the connection, you could get yourself in
a lot of transaction related messes.

You could have a file-scoped lexical in each module, with a setter function
which takes a $dbh and stores it for all future use.

You could use a global variable (e.g. $main::dbh) and refer to it by that
name in each package. But then I'd wonder of there was much point separate
modules in the first place.

Xho
 
R

Reinhard Pagitsch

Hello Roman,

Roman said:
Can anybody help? I built my script using module structure as i read: "...
and use the subroutines from modules as you are using its directly in main
program." So suppose i have the script main.pl and with 'use' i linked some
modules "Module1.pm", "Module1.pm","ModuleN.pm". The subroutines in each of
these modules use the DBI module to connect to database, make transaction,
query etc. Is there a method to create DBI object once in main.pl and use it
in any subroutine from any module. Now i have to create database connection
in each module, and there is no problem to do it because there is just 6
modules, and i can't see now how this technique slows server down, but when
the number of modules will be much more, or inet traffic will be like on
Microsoft servers :) what to do then?

use DBI;
use Module1;
my $dbh = DBI->connect(...);

$mod1 = Module1->new($dbh);


package Module1

sub new
{
my $class = shift;
my $this = {};
$this->{DBH} = shift;
bless $this,$class;
return $this;
}

now you can use $this->{DBH} in each sub of the module:

sub x
{
my $this = shift;
my $dbh = $this->{DBH}; # this is it
$dh->prepare(..);
}



mit freundlichen Grüßen,
with my best regards,
Reinhard
 
J

James Willmore

Can anybody help? I built my script using module structure as i read: "...
and use the subroutines from modules as you are using its directly in main
program." So suppose i have the script main.pl and with 'use' i linked some
modules "Module1.pm", "Module1.pm","ModuleN.pm". The subroutines in each of
these modules use the DBI module to connect to database, make transaction,
query etc. Is there a method to create DBI object once in main.pl and use it
in any subroutine from any module. Now i have to create database connection
in each module, and there is no problem to do it because there is just 6
modules, and i can't see now how this technique slows server down, but when
the number of modules will be much more, or inet traffic will be like on
Microsoft servers :) what to do then?

The connection to the database - is it the same connection for all the
packages you refer to or different databases?

If it's the same database connection, but you're performing different
queries, you can just define different scalar values for each query.

For example:

my $dbh = DBI->connect(...);

my $sth_select_dogs = $dbh->prepare(<<SQL);
select * from db where animal='dogs'
SQL

my $sth_select_cats = $dbh->prepare(<<SQL);
select * from db where animal='cats'
SQL

Now you can execute and fetch based upon the scalar for each query. And
you connected *only once* to the database :)

If you *have* to use packages, then you could pass the database handle as
a reference to your packages. I say reference because if you just pass it
as a scalar, you're basically duplicating the scalar holding the DBI
object ($dbh) and taking up space :) It's also untested, so your milage
may vary :)

For example:

--main--
my $dbh = DBI->connect( ... );

use PackageA;

my $pkg_a = PackageA::do_query(\$dbh);

--PackageA--
sub do_query{
my $dbh = shift;
my $sth = $dbh->prepare(<<SQL);
select * from db where animal='dogs';
SQL
... do stuff ...
}

In the above example, it's not, IMHO, practical to even use a package if
it's the same database connection. It's practical if you use different
databases in the same main script. You may want to file that away
somewhere for future reference :)

Just my $0.02 ....

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
If a President doesn't do it to his wife, he'll do it to his
country.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top