From: Jesse Luehrs Date: Thu, 19 May 2011 07:20:58 +0000 (-0500) Subject: better error message for calling superclasses as a writer when immutable X-Git-Tag: 2.0008~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0a6a32c5ba6c973380a23a473df935de8c54b5b8;p=gitmo%2FMoose.git better error message for calling superclasses as a writer when immutable --- diff --git a/lib/Class/MOP/Class/Immutable/Trait.pm b/lib/Class/MOP/Class/Immutable/Trait.pm index 53c80ac..6a38157 100644 --- a/lib/Class/MOP/Class/Immutable/Trait.pm +++ b/lib/Class/MOP/Class/Immutable/Trait.pm @@ -16,11 +16,9 @@ sub is_immutable { 1 } sub _immutable_metaclass { ref $_[1] } -sub superclasses { - my $orig = shift; - my $self = shift; - confess "This method is read-only" if @_; - $self->$orig; +sub _immutable_read_only { + my $name = shift; + confess "The '$name' method is read-only when called on an immutable instance"; } sub _immutable_cannot_call { @@ -28,6 +26,16 @@ sub _immutable_cannot_call { Carp::confess "The '$name' method cannot be called on an immutable instance"; } +for my $name (qw/superclasses/) { + no strict 'refs'; + *{__PACKAGE__."::$name"} = sub { + my $orig = shift; + my $self = shift; + _immutable_read_only($name) if @_; + $self->$orig; + }; +} + for my $name (qw/add_method alias_method remove_method add_attribute remove_attribute remove_package_symbol add_package_symbol/) { no strict 'refs'; *{__PACKAGE__."::$name"} = sub { _immutable_cannot_call($name) };