in reply to Using a single DBI handle in a script and a module

I'm using something like that right now, and it's not as bad as you think. Here's owtdi:

Step 1. Connect to the database, and get a database handle. You could do this in your main script, or in the module. If you do it in the module, have it return the database handle to the main script, like this:

In the module: sub george_connect { return DBI->connect('yada yada'); } In the main script: my $dbh = george_connect;
Step 2. Pass the database handle to each sub in the module as needed, e.g. select_cakes_by_type($dbh, 'battenburg');

Step 3. Disconnect in module or main script.

hth,
andy.

update: works fine with Apache::DBI

Replies are listed 'Best First'.
Re: Re: Using a single DBI handle in a script and a module
by Masem (Monsignor) on Nov 14, 2001 at 19:29 UTC
    Actually, going further with this, if you grab your dbh handle from a function in the 'main' part of the module, you can create a so-called singleton-like system, thus instead of:
    sub george_connect { return DBI->connect('yada yada'); }
    you can do:
    my $dbh; sub george_connect { if ( !$dbh ) { $dbh = DBI->connect('yada yada'); } return $dbh; }
    which maintains a handle one created, but never tried to reconnect it again.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important