Subclassing Curiosity
Seb
list at mindling.com
Mon May 6 18:48:13 CDT 2002
I have a curiosity with regard to subclassing DBI. I'm just not sure I
get the principles involved. I've got a module that is a DBI subclass.
In order to subclass DBI as "package Mine", one must also provide
packages Mine::db and Mine::st, because the database and statement
handles used in DBI are their own classes.
My confusion comes in when I put a constructor in my main package, and
try to use DBI methods on its returned object. (DBI has no constructor
method with which to reference a DBI object, per se)
So I have something like this:
#!/usr/bin/perl
package MyDBI;
use DBI;
@ISA = qw(DBI);
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
package MyDBI::db;
@ISA = qw(DBI::db);
package MyDBI::st;
@ISA = qw(DBI::st);
# now our main program
package main;
my $obj = new MyDBI;
# $obj is a MyDBI object, with DBI base methods
print '$obj is a: '.(scalar ref $obj)."\n";
my $dbh = $obj->connect('DBI:Pg:dbname=somedb', undef, undef);
# This generates a warning
# $dbh is a DBI::db object, NOT a MyDBI::db object
print '$dbh is a: '.(scalar ref $dbh)."\n";
# On the other hand, if I use the module name, not my object...
my $dbh2 = MyDBI->connect('DBI:Pg:dbname=somedb', undef, undef);
# No error, and $dbh2 is a proper MyDBI::db object
print '$dbh2 is a: '.(scalar ref $dbh2)."\n";
__END__
The output of the above code looks like this:
$obj is a: MyDBI
DBI subclass 'MyDBI=HASH(0x813c1a0)::db' isn't setup, ignored at MyDBI.pl line 26
$dbh is a: DBI::db
$dbh2 is a: MyDBI::db
<disconnect errors snipped>
Why does the first case ($obj->connect) not do what I expect it to,
while the second case (MyDBI->connect) works fine?
Is this just some basic scoping or inheritance thing that I'm not
understanding?
TIA
Sebastian
More information about the Santa-rosa-pm
mailing list