in reply to Re^6: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
in thread If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?

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, ); };
  • Comment on Re^7: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
  • Select or Download Code

Replies are listed 'Best First'.
Re^8: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
by holli (Abbot) on Oct 22, 2019 at 10:21 UTC
    Neat. Neater, see below. ;-)

    *** Trigger Warning *** Raku content *** Trigger Warning ***


    holli

    You can lead your users to water, but alas, you cannot drown them.