I didn't test the code I suggest below but may be this can provide you Some new ideas:
package Db;
use base qw(DBI);
use strict;
#------------
#Constructor
#------------
sub connect{
my($caller,@args) = @_;
my $class=ref($self) || $caller;
my $self={};
bless $self => $class;
$self->_init(@args);
return $self;
}
#--------------
#Public methods
#--------------
sub hello {
my($self) = shift;
return "Hello world";
}
sub sysdate{
my($self) = @_;
ref($self) || croak "$self is not an object\n";
my $sql="select sysdate from dual";
my $sth=$self->{DBH}->prepare($sql);
$sth->execute;
my ($sysdate)=$sth->fetchrow_array();
$sth->finish();
retrun $sysdate;
}
#----------------
#Private methods
#----------------
sub _init{
my($self,@args) = @_;
ref($self) || croak "$self is not an object\n";
#let DBI to make the connection
my $dbh=$self->Super::connect(@args);
#keep the dbh as an attribute
$self->{DBH}=$dbh;
}
1;
-- test program --
#! /usr/bin/perl
#
use Db;
use strict;
my $Dbo=Db->connect('dbi:Oracle:', 'username', 'password');
print $Dbo->sysdate(),"\n";
print $Dbo->hello(),"\n";
-- end test program --
HTH,
Jos�.
-----Original Message-----
From: John Coy [mailto:[EMAIL PROTECTED]]
Sent: Saturday, August 24, 2002 12:15 AM
To: [EMAIL PROTECTED]
Subject: how to properly extend DBI class?
Hi,
I am trying to create a new class that inherits from
DBI. However, I cannot seem to get it working properly:
-- my new class --
package Db;
use base DBI;
use strict;
sub hello {
my($self) = shift;
return "Hello world";
}
1;
-- end new class --
-- my test program --
#! /usr/bin/perl
#
use Db;
use strict;
my $dbh = Db->connect('dbi:Oracle:', 'username', 'password'); print $dbh->hello(),
"\n";
-- end test program --
When I run the test program, I get:
Can't locate object method "hello" via package "DBI::db" (perhaps you
forgot to load "DBI::db"?) at test.pl line 7.
It's clear that the $dbh object returned by Db->connect() doesn't belong to the Db
subclass I created.
As you can tell, I'm not that good with OO programming, but I'd like to learn.
Assistance is appreciated.
John
---
John Coy
CTO/VP Network Operations
ANCI/Arkansas.Net
-----Original Message-----
From: John Coy [mailto:[EMAIL PROTECTED]]
Sent: Saturday, August 24, 2002 5:49 AM
To: [EMAIL PROTECTED]
Subject: Re: how to properly extend DBI class?
>You missed a lot in building package and classes.
>Look at perlmod and perlobj documentation.
I ready very carefully much of the perl-provided documentation concerning
object-oriented programming. Thanks.
>At least you will need to set @ISA to define inheritance, and @EXPORT
>to define which item are available for the user of the package.
I address this below.
>Good luck with your codes.
..... and luck to yours as well.
It's my understanding that 'use base' accomplishes
manipulating the @ISA array. If you do a 'perldoc base' you'll see what I mean --
base.pm is a bundled module with most perl distributions. I have successfully used
'use base' with my own classes to accomplish inheritance and I have been successful.
As for @EXPORT, I read very carefully 'man perltoot' and it does say that a
"well-behaved" class doesn't export. I was attempting to make a well-behaved class.
Michael A Chase <[EMAIL PROTECTED]> gave feedback concerning the syntax of 'use
base DBI' vs. 'use base qw(DBI)'. I tried it the other way but I still get the same
result.
If it helps any, I've been programming in Perl and using DBD/DBI for quite some time.
This is just my first time trying to extend the DBI class with a custom sub-class.
Any assistance is greatly appreciated.
---
John Coy
CTO/VP Network Operations
ANCI/Arkansas.Net
**** DISCLAIMER ****
"This e-mail and any attachment thereto may contain information which is confidential
and/or protected by intellectual property rights and are intended for the sole use of
the recipient(s) named above.
Any use of the information contained herein (including, but not limited to, total or
partial reproduction, communication or distribution in any form) by other persons than
the designated recipient(s) is prohibited.
If you have received this e-mail in error, please notify the sender either by
telephone or by e-mail and delete the material from any computer".
Thank you for your cooperation.
For further information about Proximus mobile phone services please see our website at
http://www.proximus.be or refer to any Proximus agent.