Re: DBI object in modules
From: James Willmore (jwillmore_at_remove.adelphia.net)
Date: 02/26/04
- Next message: Bigus: "Re: File downloads"
- Previous message: Gunnar Hjalmarsson: "Re: How to send cookie and redirect in a cgi script?"
- In reply to: Roman Khutkyy: "DBI object in modules"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 26 Feb 2004 09:50:24 -0500
On Tue, 24 Feb 2004 17:34:58 +0200, Roman Khutkyy wrote:
> 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.
- Next message: Bigus: "Re: File downloads"
- Previous message: Gunnar Hjalmarsson: "Re: How to send cookie and redirect in a cgi script?"
- In reply to: Roman Khutkyy: "DBI object in modules"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|