From: Dagfinn Ilmari Mannsåker Date: Mon, 30 Jul 2012 21:34:34 +0000 (+0100) Subject: throw an error on non-string builder (RT#78479) X-Git-Tag: v1.000002~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=463df7d2c4547f18a633e204bda2f955d27cef30;p=gitmo%2FMoo.git throw an error on non-string builder (RT#78479) --- diff --git a/Changes b/Changes index 37ec51e..d4f729a 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - throw an error on non-string builder (RT#78479) - require D::GD 0.07 to avoid prototype mismatch errors - fix stupid typo in new Sub::Quote section diff --git a/lib/Method/Generate/Accessor.pm b/lib/Method/Generate/Accessor.pm index 7089056..c644919 100644 --- a/lib/Method/Generate/Accessor.pm +++ b/lib/Method/Generate/Accessor.pm @@ -36,6 +36,8 @@ sub generate_method { die "Unknown is ${is}"; } $spec->{builder} = '_build_'.$name if ($spec->{builder}||0) eq 1; + die "Invalid builder for $into->$name - not a string" + if exists $spec->{builder} and ref $spec->{builder}; if (($spec->{predicate}||0) eq 1) { $spec->{predicate} = $name =~ /^_/ ? "_has${name}" : "has_${name}"; } diff --git a/t/method-generate-accessor.t b/t/method-generate-accessor.t index 6914ba5..22d88af 100644 --- a/t/method-generate-accessor.t +++ b/t/method-generate-accessor.t @@ -68,6 +68,16 @@ for my $setting (qw( default coerce )) { ); } +is( + exception { $gen->generate_method('Foo' => 'ten' => { is => 'ro', builder => '_build_ten' }) }, + undef, 'builder - string accepted', +); + +like( + exception { $gen->generate_method('Foo' => 'eleven' => { is => 'ro', builder => sub {} }) }, + qr/Invalid builder/, 'builder - coderef rejected' +); + my $foo = Foo->new(one => 1); is($foo->one, 1, 'ro reads');