connect
{
my($that) = shift;
my $class = ref($that) || $that;
my $self = bless $that->SUPER::new(@_), $class;
$self->{class} = $class;
return(self);
}
??
On Thursday 19 April 2001 16:42, Paul D. Smith wrote:
> I'm trying to create a subclass of the DBI class. My goal is twofold:
>
> 1) Override the connect method so that I can hide many of the details
> of connecting from the rest of my package, and
>
> 2) Add a few extra "helper" methods.
>
> I want to access my extra helper methods, plus all the standard DBI
> methods.
>
> My subclass has no data of its own, so I just wanted to use the normal
> DBI handle as the object reference.
>
> I did something like this:
>
> package My::DB;
>
> use strict;
> use vars qw(@ISA);
>
> use DBI;
>
> @ISA = qw(DBI);
>
> sub connect {
> my $self = shift;
> my $class = ref($self) || $self;
>
> #... figure connect info $data_source, $username, $password
>
> return $class->SUPER::connect($data_source, $username, $password);
> }
>
>
> sub MyMethod {
> my $this = shift;
>
> # ... do some stuff
> }
>
> Now when I do this:
>
> $dbh = My::DB->connect();
>
> it works, but this:
>
> $dbh->MyMethod();
>
> fails with "Can't locate object method "MyMethod" via package My::DB at
> ...". Can I not inherit from DBI? I thought that the reference would
> be for my class, if DBI uses the two-argument form of bless... What am
> I doing wrong here?
>
> So, then I thought I'd try re-blessing it into my class, like this:
>
> sub connect {
> my $self = shift;
> my $class = ref($self) || $self;
>
> #... figure connect info $data_source, $username, $password
>
> my $this = $class->SUPER::connect($data_source, $username, $password);
>
> return bless($this, $class);
> }
>
> Now I can get to $dbh->MyMethod(), but I can't get to any of the DBI
> methods; for example $dbh->prepare() fails with:
>
> Can't locate auto/My/DB/prepare.al in @INC ...
>
> I can't even get there using SUPER; this also fails:
>
> sub MyMethod {
> my $this = shift;
>
> # ... do some stuff
>
> return $this->SUPER::prepare(...);
> }
>
> Gives me:
>
> Can't locate auto/My/DB/SUPER/prepare.al in @INC ...
>
> which seems strange to me.
>
> Anyway, can anyone help me make a subclass of DBI that does what I want?
> Do I have to write some kind of AUTOLOAD to do a delegator, as described
> in Camel? I was hoping that the @ISA thing would allow any method that
> wasn't known in my class to be automatically looked for in my parent
> classes... why doesn't that work?
>
> Thanks!
--
------------------------------------
|| James Maes
|| Senior Programmer
|| [EMAIL PROTECTED]
|| The Sporting News
|| www.sportingnews.com
|| fantasy.sportingnews.com
------------------------------------
"given enough time and resources we can accomplish anything"