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!
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
|
Ah, I've stumbled over this: Parameters which are only needed at object creation, but which don't need to stick around (or shouldn't, in the case of a password).
Contrary to my previous post, where I advocated to use this only to keep APIs compatible, this is another example where I find coercion to be an excellent tool. Something sort of like this (note I'm not familiar with Moo coercion which seems to be different from Moose coercion, so caution is advised):
package Moo::Role::DBIConnection;
use Moo::Role;
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 => 'ro',
coerce => \&_coerce_db,
);
sub _coerce_db {
my $connection_data = shift;
return UNIVERSAL::isa($connection_data,'DBI::db') ?
$connection_data :
DBI->connect( @$connection_data{qw(dsn user password options)}
+ );
}
1;
Edited to add: 1nickt was faster and more comprehensive with the same proposal. I upvoted his, feel free to ignore mine.
| [reply] [d/l] |
|
"feel free to ignore mine"
au contraire! Your solution is identical ... down to the hashref slice and the lack of parameter checking (allowing DBI to raise whatever its exception is). I upvoted your post too and I'm pleased to see it, not least because it makes me more confident my suggested approach was a good one. Keep posting!
The way forward always starts with a minimal test.
| [reply] |
|
Thank you both for this example! I think it will help me move much of this kind of stuff away from accessors.
Now that you have mentioned this, I seem to remember something similar being mentioned in that talk, but only as "What he really wants are coercions", but that mention wasn't made loud or raised for discussion. In any case, your posts showed me how Moo* wants to be used for building nicer constructors and how I can eliminate the bad part of my constructors without resorting to the real manual parsing of the constructor arguments again.
| [reply] |
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
|
I think I would use a coercion to simplify that, and, yes, shed the unwanted attributes (accessors). Note I dropped the signatures as I don;t have the Filter module installed, but also no longer needed.
package Moo::Role::DBIConnection {
use Moo::Role;
use DBI;
has 'dbh' => (
is => 'ro',
required => 1,
coerce => sub {
my $args = shift;
return $args if ref($args) eq 'DBI::db';
ref($args) eq 'HASH' or die 'Not a DB handle nor a hashref
+';
return DBI->connect( @{$args}{qw/dsn user password options
+/} );
},
);
};
##
# testing
package MyClass {
use Moo;
with 'Moo::Role::DBIConnection';
};
use Test::Most 'die';
my %args = (
dsn => 'dbi:mysql:database=mysql',
user => 'ntonkin',
password => undef,
options => { RaiseError => 1 },
);
subtest 'With no args' => sub {
dies_ok sub { my $o = MyClass->new }, 'exception on no args';
};
subtest 'With bad type' => sub {
throws_ok { my $o = MyClass->new(dbh => [\%args]) } qr/Not a DB ha
+ndle nor a hashref/;
};
subtest 'With existing handle' => sub {
my $dbh = DBI->connect( @args{qw/dsn user password options/} );
cmp_ok( $dbh->do('select count(*) from db'), '>', 0, 'Found a DB'
+);
my $o = new_ok('MyClass', [dbh => $dbh], 'No exception with handle
+ passed in');
cmp_ok( $o->dbh->do('select count(*) from db'), '>', 0, 'Found a D
+B via obj');
};
subtest 'With bad params' => sub {
local $args{user} = 'frobnicator';
throws_ok { my $o = MyClass->new(dbh => \%args) } qr/coercion for
+"dbh" failed/;
throws_ok { my $o = MyClass->new(dbh => \%args) } qr/Access denied
+ for user/;
};
subtest 'With params' => sub {
my $o = new_ok('MyClass', [dbh => \%args], 'No exception with args
+ hash passed in');
cmp_ok( $o->dbh->do('select count(*) from db'), '>', 0, 'Found a D
+B via obj');
dies_ok sub { print $o->dsn }, 'No DSN accessor!';
};
done_testing;
Output:
$ prove -lrv 11107494.pl
11107494.pl ..
# Subtest: With no args
ok 1 - exception on no args
1..1
ok 1 - With no args
# Subtest: With bad type
ok 1 - threw Regexp ((?^:Not a DB handle nor a hashref))
1..1
ok 2 - With bad type
# Subtest: With existing handle
ok 1 - Found a DB
ok 2 - 'No exception with handle passed in' isa 'MyClass'
ok 3 - Found a DB via obj
1..3
ok 3 - With existing handle
# Subtest: With bad params
ok 1 - threw Regexp ((?^:coercion for "dbh" failed))
ok 2 - threw Regexp ((?^:Access denied for user))
1..2
ok 4 - With bad params
# Subtest: With params
ok 1 - 'No exception with args hash passed in' isa 'MyClass'
ok 2 - Found a DB via obj
ok 3 - No DSN accessor!
1..3
ok 5 - With params
1..5
ok
All tests successful.
Files=1, Tests=5, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.09 cusr
+ 0.01 csys = 0.11 CPU)
Result: PASS
Hope this helps!
update: added test for bad type and moved isa check to coercion
The way forward always starts with a minimal test.
| [reply] [d/l] [select] |
|
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,
);
};
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
|