Use dzil Authority plugin - remove $AUTHORITY from code
[gitmo/Moose.git] / lib / Class / MOP / Class / Immutable / Trait.pm
CommitLineData
38bf2a25 1package Class::MOP::Class::Immutable::Trait;
2
3use strict;
4use warnings;
5
6use MRO::Compat;
7
8use Carp 'confess';
9use Scalar::Util 'blessed', 'weaken';
10
38bf2a25 11# the original class of the metaclass instance
12sub _get_mutable_metaclass_name { $_[0]{__immutable}{original_class} }
13
14sub is_mutable { 0 }
15sub is_immutable { 1 }
16
17sub _immutable_metaclass { ref $_[1] }
18
19sub superclasses {
20 my $orig = shift;
21 my $self = shift;
22 confess "This method is read-only" if @_;
23 $self->$orig;
24}
25
26sub _immutable_cannot_call {
27 my $name = shift;
28 Carp::confess "The '$name' method cannot be called on an immutable instance";
29}
30
31for my $name (qw/add_method alias_method remove_method add_attribute remove_attribute remove_package_symbol add_package_symbol/) {
32 no strict 'refs';
33 *{__PACKAGE__."::$name"} = sub { _immutable_cannot_call($name) };
34}
35
36sub class_precedence_list {
37 my $orig = shift;
38 my $self = shift;
39 @{ $self->{__immutable}{class_precedence_list}
40 ||= [ $self->$orig ] };
41}
42
43sub linearized_isa {
44 my $orig = shift;
45 my $self = shift;
46 @{ $self->{__immutable}{linearized_isa} ||= [ $self->$orig ] };
47}
48
49sub get_all_methods {
50 my $orig = shift;
51 my $self = shift;
52 @{ $self->{__immutable}{get_all_methods} ||= [ $self->$orig ] };
53}
54
55sub get_all_method_names {
56 my $orig = shift;
57 my $self = shift;
58 @{ $self->{__immutable}{get_all_method_names} ||= [ $self->$orig ] };
59}
60
61sub get_all_attributes {
62 my $orig = shift;
63 my $self = shift;
64 @{ $self->{__immutable}{get_all_attributes} ||= [ $self->$orig ] };
65}
66
67sub get_meta_instance {
68 my $orig = shift;
69 my $self = shift;
70 $self->{__immutable}{get_meta_instance} ||= $self->$orig;
71}
72
73sub _method_map {
74 my $orig = shift;
75 my $self = shift;
76 $self->{__immutable}{_method_map} ||= $self->$orig;
77}
78
791;
80
81# ABSTRACT: Implements immutability for metaclass objects
82
83__END__
84
85=pod
86
87=head1 DESCRIPTION
88
89This class provides a pseudo-trait that is applied to immutable metaclass
90objects. In reality, it is simply a parent class.
91
92It implements caching and read-only-ness for various metaclass methods.
93
94=cut
95