if (($spec->{trigger}||0) eq 1) {
$spec->{trigger} = quote_sub('shift->_trigger_'.$name.'(@_)');
}
+ if (exists $spec->{default}) {
+ if (not ref $spec->{default}) {
+ die "Invalid default $spec->{default}";
+ }
+ elsif (ref $spec->{default} ne 'CODE') {
+ require Scalar::Util;
+ die "Invalid default $spec->{default}" unless Scalar::Util::blessed $spec->{default};
+ }
+ }
+
my %methods;
if (my $reader = $spec->{reader}) {
if (our $CAN_HAZ_XS && $self->is_simple_get($name, $spec)) {
qr/Unknown is purple/, 'is purple rejected'
);
+like(
+ exception { $gen->generate_method('Foo' => 'four' => { is => 'ro', default => 5 }) },
+ qr/Invalid default/, 'default scalar rejected'
+);
+
+like(
+ exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => [] }) },
+ qr/Invalid default/, 'default arrayref rejected'
+);
+
+is(
+ exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', default => sub { 5 } }) },
+ undef, 'default coderef accepted'
+);
+
+is(
+ exception { $gen->generate_method('Foo' => 'seven' => { is => 'ro', default => bless sub { 5 } => 'Blah' }) },
+ undef, 'default blessed sub accepted'
+);
+
my $foo = Foo->new(one => 1);
is($foo->one, 1, 'ro reads');