From: Christian Walde Date: Sat, 14 Jul 2012 13:56:13 +0000 (+0200) Subject: useful and detailed errors for default checker in attrib generation X-Git-Tag: v0.091012~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3202e039c2dfaf3905404271c2b92aea38e68efc;p=gitmo%2FMoo.git useful and detailed errors for default checker in attrib generation --- diff --git a/Changes b/Changes index 1e08e65..6c62557 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ + - useful and detailed errors for default checker in attrib generation + 0.091011 - 2012-06-27 - re-add #web-simple as development IRC - don't assume Scalar::Util is imported into the current package diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index 88e38a3..1268a9e 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -5,6 +5,7 @@ use Moo::_Utils; use base qw(Moo::Object); use Sub::Quote; use B 'perlstring'; +use Scalar::Util 'blessed'; BEGIN { our $CAN_HAZ_XS = !$ENV{MOO_XS_DISABLE} @@ -45,9 +46,12 @@ sub generate_method { } if (exists $spec->{default}) { my $default = $spec->{default}; - # default can be either a coderef or an overloaded object - die "Invalid default $default" unless ref $default - and ( ref $default eq 'CODE' or eval { \&$default } ); + my $invalid = "Invalid default '" . overload::StrVal($default) + . "' for $into->$name - not a coderef"; + die "$invalid or code-convertible object" + unless ref $default and (ref $default eq 'CODE' or blessed($default)); + die "$invalid and could not be converted to a coderef: $@" + if !eval { \&$default }; } my %methods; diff --git a/lib/Moo.pm b/lib/Moo.pm index 76ed286..3e256e2 100644 --- a/lib/Moo.pm +++ b/lib/Moo.pm @@ -706,6 +706,8 @@ doy - Jesse Luehrs (cpan:DOY) perigrin - Chris Prather (cpan:PERIGRIN) +Mithaldu - Christian Walde (cpan:MITHALDU) + =head1 COPYRIGHT Copyright (c) 2010-2011 the Moo L and L diff --git a/t/method-generate-accessor.t b/t/method-generate-accessor.t index 7043a4b..f66acfb 100644 --- a/t/method-generate-accessor.t +++ b/t/method-generate-accessor.t @@ -35,6 +35,11 @@ like( qr/Invalid default/, 'default - arrayref rejected' ); +like( + exception { $gen->generate_method('Foo' => 'five' => { is => 'ro', default => Foo->new }) }, + qr/Invalid default/, 'default - non-code-convertible object rejected' +); + is( exception { $gen->generate_method('Foo' => 'six' => { is => 'ro', default => sub { 5 } }) }, undef, 'default - coderef accepted'