why does default not trigger coerce?
[email protected] (benh)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
ok this is an overly simplistic example of what I'm trying to do but
in the end I was expecting that default would trigger coerce, though
it looks like it does not. Should it? or am I expecting something to
work in a completely stupid way (it's happened before)?
#!/usr/bin/perl;
use strict;
use warnings;
use Test::Most qw{no_plan bail};
BEGIN {
package My::Test;
use Moose;
use Scalar::Util qw{looks_like_number};
use Moose::Util::TypeConstraints;
subtype 'split_types' => as 'HashRef[ArrayRef]';
coerce 'split_types'
=> from 'ArrayRef'
=> via { _split_em(@$_) };
sub _split_em {
return { nums => [ grep{ looks_like_number($_) } @_ ],
strs => [ grep{!looks_like_number($_) } @_ ],
};
}
has data => (
is => 'rw',
isa => 'split_types',
coerce => 1,
default => sub{ () },
);
} #END BEGIN
eq_or_diff(
My::Test::_split_em(1,2,3),
{ nums => [1,2,3],
strs => [],
},
);
eq_or_diff(
My::Test::_split_em(qw{cat dog}),
{ nums => [],
strs => [qw{cat dog}],
},
);
eq_or_diff(
My::Test::_split_em(1,2,3,qw{cat dog}),
{ nums => [1,2,3],
strs => [qw{cat dog}],
},
);
foreach my $in ( [1,2,3], [qw{cat dog}], [qw{1 2 3 cat dog}] ) {
ok( my $t = My::Test->new( data => [1,2,3] ) );
}
# THIS TEST FAILS!
lives_ok { My::Test->new }, q{~shouldn't~ this pass?};
--
benh~
http://three.sentenc.es/