$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
- 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));
- die "$invalid and could not be converted to a coderef: $@"
- if !eval { \&$value };
+ for my $setting (qw( isa coerce )) {
+ next if !exists $spec->{$setting};
+ $self->_validate_codulatable($setting, $spec->{$setting}, "$into->$name");
}
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 };
+ if (!defined $spec->{default} || ref $spec->{default}) {
+ $self->_validate_codulatable('default', $spec->{default}, "$into->$name", 'or a non-ref');
}
}
+ if (exists $spec->{moosify}) {
+ if (ref $spec->{moosify} ne 'ARRAY') {
+ $spec->{moosify} = [$spec->{moosify}];
+ }
+
+ for my $spec (@{$spec->{moosify}}) {
+ $self->_validate_codulatable('moosify', $spec, "$into->$name");
+ }
+ }
my %methods;
if (my $reader = $spec->{reader}) {
sub has_eager_default {
my ($self, $name, $spec) = @_;
- (!$spec->{lazy} and ($spec->{default} or $spec->{builder}));
+ (!$spec->{lazy} and (exists $spec->{default} or $spec->{builder}));
}
sub _generate_get {
my ($self, $name, $type, $values, $sub) = @_;
$sub = \&{$sub} if blessed($sub); # coderef if blessed
if (my $quoted = quoted_from_sub($sub)) {
+ my $local = 1;
+ if ($values eq '@_' || $values eq '$_[0]') {
+ $local = 0;
+ $values = '@_';
+ }
my $code = $quoted->[1];
if (my $captures = $quoted->[2]) {
my $cap_name = qq{\$${type}_captures_for_${name}};
$self->{captures}->{$cap_name} = \$captures;
Sub::Quote::inlinify(
- $code, $values, Sub::Quote::capture_unroll($cap_name, $captures, 6), 1
+ $code, $values, Sub::Quote::capture_unroll($cap_name, $captures, 6), $local
);
} else {
- Sub::Quote::inlinify($code, $values, undef, 1);
+ Sub::Quote::inlinify($code, $values, undef, $local);
}
} else {
my $cap_name = qq{\$${type}_for_${name}};
sub default_construction_string { '{}' }
+sub _validate_codulatable {
+ my ($self, $setting, $value, $into, $appended) = @_;
+ my $invalid = "Invalid $setting '" . overload::StrVal($value)
+ . "' for $into not a coderef";
+ $invalid .= " $appended" if $appended;
+
+ unless (ref $value and (ref $value eq 'CODE' or blessed($value))) {
+ die "$invalid or code-convertible object";
+ }
+
+ unless (eval { \&$value }) {
+ die "$invalid and could not be converted to a coderef: $@";
+ }
+
+ 1;
+}
+
1;