in reply to RE: passing DBI database handles to subroutines
in thread passing DBI database handles to subroutines

Make sure your named subroutine is inside a BEGIN block, like so:
BEGIN { my $dbh; sub get_db_handle { .. .. return $dbh; } }
If you fail to do that, and manage executing that block twice in a program, you'll get the wonderful $dbh won't stay shared error, indicating some serious bad voodoo.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE (3): passing DBI database handles to subroutines
by tilly (Archbishop) on Aug 09, 2000 at 23:20 UTC
    Right you are of course.

    OTOH another way to guarantee the right behaviour is to put the subroutine in a module. And that module not coincidentally in my case is where the name and password are, making it easy to change the password periodically.

    Of course dealing with mod_perl you probably get to curse the "won't stay shared" error much more than I do. :-)

      With mod_perl, you are likely best just making $dbh a package var of a "library module", that everything use's Then the "won't stay shared" is avoided.
      I have quite a bit of code that does this, basically its just wrappers on all the database calls that automagically connect and stay connected, but only a single connection required for each apache process.
        That depends. In this case my consideration was that I did not want someone thinking that they should access that variable directly, and making the connection logic implicit to needing the database simplified initialization logic.

        Making it impossible to even try to make that mistake makes it less likely that I will. :-)