1 package Class::MOP::Class::Immutable::Trait;
9 use Scalar::Util 'blessed', 'weaken';
11 # the original class of the metaclass instance
12 sub _get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
15 sub is_immutable { 1 }
17 sub _immutable_metaclass { ref $_[1] }
19 sub _immutable_read_only {
21 confess "The '$name' method is read-only when called on an immutable instance";
24 sub _immutable_cannot_call {
26 Carp::confess "The '$name' method cannot be called on an immutable instance";
29 for my $name (qw/superclasses/) {
31 *{__PACKAGE__."::$name"} = sub {
34 _immutable_read_only($name) if @_;
39 for my $name (qw/add_method alias_method remove_method add_attribute remove_attribute remove_package_symbol add_package_symbol/) {
41 *{__PACKAGE__."::$name"} = sub { _immutable_cannot_call($name) };
44 sub class_precedence_list {
47 @{ $self->{__immutable}{class_precedence_list}
48 ||= [ $self->$orig ] };
54 @{ $self->{__immutable}{linearized_isa} ||= [ $self->$orig ] };
60 @{ $self->{__immutable}{get_all_methods} ||= [ $self->$orig ] };
63 sub get_all_method_names {
66 @{ $self->{__immutable}{get_all_method_names} ||= [ $self->$orig ] };
69 sub get_all_attributes {
72 @{ $self->{__immutable}{get_all_attributes} ||= [ $self->$orig ] };
75 sub get_meta_instance {
78 $self->{__immutable}{get_meta_instance} ||= $self->$orig;
84 $self->{__immutable}{_method_map} ||= $self->$orig;
89 # ABSTRACT: Implements immutability for metaclass objects
97 This class provides a pseudo-trait that is applied to immutable metaclass
98 objects. In reality, it is simply a parent class.
100 It implements caching and read-only-ness for various metaclass methods.