metaperl has asked for the wisdom of the Perl Monks concerning the following question:

When writing a (Moose)? app that will require a database connection, how do you structure your class/leverage a CPAN module so that it has the $dbh it needs whether it was called as:
my $object = Class->new(dbh => $dbh);
or via
my $object = Class->new(dsn => $dsn, user=>$user, pass=>$pass);




The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

Replies are listed 'Best First'.
Re: (Moose)? $dbh or DBI connection info - how to abstract
by Corion (Patriarch) on Jun 24, 2010 at 13:50 UTC

    I use both, as I need to pass in a pre-existing dbh for testing but might want to create a different database connection in other situations.

Re: (Moose)? $dbh or DBI connection info - how to abstract
by stvn (Monsignor) on Jun 24, 2010 at 16:44 UTC

    You should take a look at DBIx::Connection because it solves a lot of the issues with handling $dbh instances. I recommend you pass around the DBIx::Connection object, but if you want just the $dbh, you can use Moose coercions to convert a DBIx::Connection object into a $dbh.

    -stvn