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} } ... #### my $db = My::DBI->new(connect => { ... }); $db->my_function(@args);