$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
if (exists $spec->{default}) {
- if (not ref $spec->{default}) {
- die "Invalid default $spec->{default}";
+ my $default = $spec->{default};
+ require Scalar::Util;
+ if (not ref $default) {
+ die "Invalid default $default";
}
- elsif (ref $spec->{default} ne 'CODE') {
- require Scalar::Util;
- die "Invalid default $spec->{default}" unless Scalar::Util::blessed $spec->{default};
+ elsif (Scalar::Util::reftype $default ne 'CODE') {
+ if (Scalar::Util::blessed $default) {
+ die "Invalid default $default" unless $default->can('(&{}');
+ }
+ else {
+ die "Invalid default $default";
+ }
}
}
like(
exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) },
- qr/Invalid default/, 'default scalar rejected'
+ qr/Invalid default/, 'default - scalar rejected'
);
like(
exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => [] }) },
- qr/Invalid default/, 'default arrayref rejected'
+ qr/Invalid default/, 'default - arrayref rejected'
);
is(
exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', default => sub { 5 } }) },
- undef, 'default coderef accepted'
+ undef, 'default - coderef accepted'
);
is(
exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', default => bless sub { 5 } => 'Blah' }) },
- undef, 'default blessed sub accepted'
+ undef, 'default - blessed sub accepted'
+);
+
+{
+ package WithOverload;
+ use overload '&{}' => sub { sub { 5 } };
+ sub new { bless {} }
+}
+
+is(
+ exception { $gen->generate_method('Foo' => 'eight' => { is => 'ro', default => WithOverload->new }) },
+ undef, 'default - object with overloaded ->() accepted'
+);
+
+like(
+ exception { $gen->generate_method('Foo' => 'nine' => { is => 'ro', default => bless {} => 'Blah' }) },
+ qr/Invalid default/, 'default - object rejected'
);
my $foo = Foo->new(one => 1);