From: Shawn M Moore Date: Tue, 10 Jun 2008 02:57:38 +0000 (+0000) Subject: Moose compat: throw an error on $self->reader(value) X-Git-Tag: 0.04~65 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=636c002e5c9f843ad7c7cfb35a1511b4ccbb562f;p=gitmo%2FMouse.git Moose compat: throw an error on $self->reader(value) --- diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index 1a971bf..b5dfb1d 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -78,6 +78,7 @@ sub generate_accessor { $accessor .= '}'; } else { + $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;'; } if ($attribute->is_lazy) { diff --git a/t/007-attributes.t b/t/007-attributes.t index cf2b5a7..4316e25 100644 --- a/t/007-attributes.t +++ b/t/007-attributes.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More tests => 10; +use Test::Exception; do { package Class; @@ -27,7 +28,11 @@ ok(!$object->can('x'), "No accessor is injected if 'is' has no value"); can_ok($object, 'y', 'z'); is($object->y, undef); -is($object->y(10), undef); + +throws_ok { + $object->y(10); +} qr/Cannot assign a value to a read-only accessor/; + is($object->y, undef); is($object->z, undef);