in reply to Re^4: 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?
Yes, I could do that all in BUILDARGS, but "look at how tedious that is", when compared with doing the stuff as (fake) accessors. In a way this feels to me like Java where every data structure has to become a class.
The following is a helper (role) that merely ingests either a premade dbh, or otherwise, a db username, db password and a DSN, to use DBI to make the DBH from:
package Moo::Role::DBIConnection; use Moo::Role; use Filter::signatures; use feature 'signatures'; no warnings 'experimental::signatures'; use DBI; our $VERSION = '0.01'; =head1 NAME Moo::Role::DBIConnection =head1 SYNOPSIS { package My::Example; use Moo 2; with 'Moo::Role::DBIConnection'; }; # Connect using the parameters my $writer = My::Example->new( dbh => { dsn => '...', user => '...', password => '...', options => '...', }, ); # ... or alternatively if you have a connection already my $writer2 = My::Example->new( dbh => $dbh, ); =cut has 'dbh' => ( is => 'lazy', default => \&_connect_db, ); has 'dsn' => ( is => 'ro', ); has 'user' => ( is => 'ro', ); has 'password' => ( is => 'ro', ); has 'options' => ( is => 'ro', ); sub _connect_db( $self ) { my $dbh = DBI->connect( $self->dsn, $self->user, $self->password, $self->options ); } 1;
Somehow, I think shouldn't have to make the dsn, name and password accessors just so I can use them from _connect_db, and ideally, there would be a nicer thing that implements what the Moo*-supplied ->new() does, except that it expects just a hashref, instead of expecting accessors on $self.
But I haven't seen that yet, so it's not really a feature I'd want to see in Perl 7. If it happens for Perl 7 that would be doubly great, of course!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
by haj (Vicar) on Oct 15, 2019 at 20:14 UTC | |
by 1nickt (Canon) on Oct 16, 2019 at 01:32 UTC | |
by Corion (Patriarch) on Oct 16, 2019 at 07:32 UTC | |
Re^6: If Perl 5 were to become Perl 7, what (backward-compatible) features would you want to see?
by 1nickt (Canon) on Oct 15, 2019 at 20:03 UTC | |
by tobyink (Canon) on Oct 16, 2019 at 07:29 UTC | |
by holli (Abbot) on Oct 22, 2019 at 10:21 UTC |