$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
- for my $setting (qw( default coerce )) {
- next if !exists $spec->{$setting};
- my $value = $spec->{$setting};
- my $invalid = "Invalid $setting '" . overload::StrVal($value)
+ if (exists $spec->{coerce}) {
+ my $value = $spec->{coerce};
+ my $invalid = "Invalid coerce '" . overload::StrVal($value)
. "' for $into->$name - not a coderef";
die "$invalid or code-convertible object"
unless ref $value and (ref $value eq 'CODE' or blessed($value));
if !eval { \&$value };
}
+ if (exists $spec->{default}) {
+ my $value = $spec->{default};
+ if (!defined $value || ref $value) {
+ my $invalid = "Invalid default '" . overload::StrVal($value)
+ . "' for $into->$name - not a coderef or non-ref";
+ die "$invalid or code-convertible object"
+ unless ref $value and (ref $value eq 'CODE' or blessed($value));
+ die "$invalid and could not be converted to a coderef: $@"
+ if !eval { \&$value };
+ }
+ }
+
+
my %methods;
if (my $reader = $spec->{reader}) {
if (our $CAN_HAZ_XS && $self->is_simple_get($name, $spec)) {
sub _generate_get_default {
my ($self, $me, $name, $spec) = @_;
- $spec->{default}
- ? $self->_generate_call_code($name, 'default', $me, $spec->{default})
- : "${me}->${\$spec->{builder}}"
+ if (exists $spec->{default}) {
+ ref $spec->{default}
+ ? $self->_generate_call_code($name, 'default', $me, $spec->{default})
+ : perlstring $spec->{default};
+ }
+ else {
+ "${me}->${\$spec->{builder}}"
+ }
}
sub generate_simple_get {
sub _build_eight { {} }
has nine => (is => 'lazy', coerce => sub { $c_ran = 1; $_[0] });
sub _build_nine { {} }
+ has ten => (is => 'lazy', default => 5 );
}
sub check {
check nine => map Foo->new->nine, 1..2;
ok($c_ran, 'coerce lazy default');
+is(Foo->new->ten, 5, 'non-ref default');
+
done_testing;
qr/Unknown is purple/, 'is purple rejected'
);
-for my $setting (qw( default coerce )) {
- like(
- exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', $setting => 5 }) },
- qr/Invalid $setting/, "$setting - scalar rejected"
- );
+like(
+ exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', coerce => 5 }) },
+ qr/Invalid coerce/, "coerce - scalar rejected"
+);
+is(
+ exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) },
+ undef, "default - non-ref scalar accepted"
+);
+
+
+for my $setting (qw( default coerce )) {
like(
exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', $setting => [] }) },
qr/Invalid $setting/, "$setting - arrayref rejected"