package Foo; ... our $error; *DBI::carp = sub { $Foo::error = shift }; ... my $self->{dbh} = DBI->connect( ... ) or carp $error;