If you're happy to coerce from an arrayref ([$dsn,$u,$p,\%opts]) instead of a hashref, then:
package Moo::Role::DBIConnection { use Moo::Role; use DBI; use Types::DBI; has dbh => (is => 'ro', isa => Dbh, required => 1, coerce => 1); };
Coercion from HashRef isn't provided in Types::DBI (because that would require choosing hash key names, something probably application-specific) but it's pretty easy to add in if you need it:
package Moo::Role::DBIConnection { use Moo::Role; use DBI; use Types::DBI; use Types::Standard qw(HashRef); has dbh => ( is => 'ro', isa => Dbh->plus_coercions(HashRef, sub { DBI->connect( +@{$_}{qw/dsn user password options/}) }), required => 1, coerce => 1, ); };
In reply to Re^7: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
by tobyink
in thread If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
by haukex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |