[Israel.pm] Class::DBI with several databases from the same script

Gabor Szabo gabor at perl.org.il
Sat May 15 05:11:52 PDT 2004


I am trying to use Class::DBI with several databases from the same
script. All the databases have the same Schema so I use the same
Class::DBI subclass for all of them.

In MyProject::DBI I have the following code I call after the user
gives me which database to use:

  __PACKAGE__->set_db('Main', "dbi:SQLite:$dbfile", 'root', undef,
                   {
		    ChopBlanks => 0,
                    sqlite_handle_binary_nulls  => 1,
		    });


When I do this for the first time, everything works fine.

When - in the same run of the code - I do this for the second time
it still works fine but also complains

Subroutine MyProject::DBI::db_Main redefined at .....

So how can I eliminate that ?


Solution 1)
===========
No problem, I can use different database names for each
database instead of "Main" so instead of db_Main Ima::DBI will
create db_A  db_B etc....

.... well, not exactly.
Looking at the source of Class::DBI I found out that it actually does
not care what name do I give as the first parameter to set_db,
it always passes "Main" to the set_db method of Ima::DBI.
Can anyone explain why ? Is this the only way CDBI can work ?


Class::DBI 0.96
Ima::DBI 0.33
Perl 5.8.4   [1]

Solution 2)
===========
I can use different subclass for each one of my databases
somehow replacing MyProject::DBI with A::DBI, B::DBI etc
on the fly but I have not done that yet and I don't feel enough
lazy today to try to do that.

Solution 3)
===========
My solution for now is to override $SIG{__WARN__} and eliminate all
the warning regarding redefining subroutines.

  local $SIG{__WARN__} = sub {
       if ($_[0] !~ /^Subroutine MyProject::DBI::db_Main redefined at/) {
          warn $_[0];
       }
  };

This solution works well for now but I think I'll have to find a
better solution as this one has something to do with sweeping
and carpets.



Gabor
[1] BTW under 5.8.2 I don't get these warnings.



More information about the Perl mailing list