in reply to need help developing fly OO style
Here's what I've done in the past, which makes it easier. I use a "has-a" relationship rather than an "is-a" relationship.
I've created a My::DBI class (or something such). An object of type My::DBI contains a DBI database handle; but it's not actually a database handle itself. Every method that I call on my database handle is forwarded to a call on the database handle; before and after this forwarding I can do some processing on my own. For example (this code is untested, though it's based from memory on code I've written before, which *is* tested):
And so on. Usage:package My::DBI; use strict; sub new { my $class = shift; bless { @_ }, $class; } sub connect { my $self = shift; return $self->{dbh} if defined $self->{dbh}; croak "Need connection parameters" unless defined $self->{connect}; my $c = $self->{connect}; ## Simplify typing :) return $self->{dbh} = DBI->connect ($c->{dsn}, $c->{user}, $c->{pass}, 'mysql', { RaiseError => 1 }); } sub my_function { my $self = shift; ## $DBH is now in $self->{dbh} } ...
Would that work for you?my $db = My::DBI->new(connect => { ... }); $db->my_function(@args);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: My::DBI solution
by markjugg (Curate) on Aug 12, 2000 at 12:25 UTC |