class-module-package
[gitmo/Class-MOP.git] / lib / Class / MOP / Class / Immutable.pm
CommitLineData
857f87a7 1
2package Class::MOP::Class::Immutable;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10our $VERSION = '0.01';
11
12use base 'Class::MOP::Class';
13
14# methods which can *not* be called
15
16sub reinitialize { confess 'Cannot call method "reinitialize" on an immutable instance' }
17
18sub add_method { confess 'Cannot call method "add_method" on an immutable instance' }
19sub alias_method { confess 'Cannot call method "alias_method" on an immutable instance' }
20sub remove_method { confess 'Cannot call method "remove_method" on an immutable instance' }
21
22sub add_attribute { confess 'Cannot call method "add_attribute" on an immutable instance' }
23sub remove_attribute { confess 'Cannot call method "remove_attribute" on an immutable instance' }
24
25sub add_package_variable { confess 'Cannot call method "add_package_variable" on an immutable instance' }
26sub remove_package_variable { confess 'Cannot call method "remove_package_variable" on an immutable instance' }
27
28# NOTE:
29# superclasses is an accessor, so
30# it just cannot be changed
31sub superclasses {
32 my $class = shift;
33 (!@_) || confess 'Cannot change the "superclasses" on an immmutable instance';
34 no strict 'refs';
35 @{$class->name . '::ISA'};
36}
37
38# predicates
39
40sub is_mutable { 0 }
41sub is_immutable { 1 }
42
43sub make_immutable { () }
44
45sub make_metaclass_immutable {
46 my ($class, $metaclass) = @_;
47 $metaclass->{'___class_precedence_list'} = [ $metaclass->class_precedence_list ];
48 $metaclass->{'___get_meta_instance'} = $metaclass->get_meta_instance;
49 $metaclass->{'___compute_all_applicable_attributes'} = [ $metaclass->compute_all_applicable_attributes ];
50 $metaclass->{'___original_class'} = blessed($metaclass);
51 bless $metaclass => $class;
52}
53
54# cached methods
55
56sub get_meta_instance { (shift)->{'___get_meta_instance'} }
57
58sub class_precedence_list {
59 @{ (shift)->{'___class_precedence_list'} }
60}
61
62sub compute_all_applicable_attributes {
63 @{ (shift)->{'___compute_all_applicable_attributes'} }
64}
65
661;
67
68__END__
69
70=pod
71
72=head1 NAME
73
74Class::MOP::Class::Immutable - An immutable version of Class::MOP::Class
75
76=head1 SYNOPSIS
77
78=head1 DESCRIPTION
79
80=head1 METHODS
81
82=over 4
83
84=item B<meta>
85
86This will return a B<Class::MOP::Class> instance which is related
87to this class.
88
89=back
90
2243a22b 91=head2 Methods which will die if you touch them.
92
93=over 4
94
95=item B<add_attribute>
96
97=item B<add_method>
98
99=item B<add_package_variable>
100
101=item B<alias_method>
102
103=item B<reinitialize>
104
105=item B<remove_attribute>
106
107=item B<remove_method>
108
109=item B<remove_package_variable>
110
111=item B<superclasses>
112
113=back
114
115=head2 Cached methods
116
857f87a7 117=over 4
118
2243a22b 119=item B<class_precedence_list>
120
121=item B<compute_all_applicable_attributes>
122
123=item B<get_meta_instance>
124
125=back
126
127=head2 Introspection and Construction
128
129=over 4
130
131=item B<is_immutable>
132
133=item B<is_mutable>
134
135=item B<make_immutable>
136
137=item B<make_metaclass_immutable>
857f87a7 138
139=back
140
141=head1 AUTHOR
142
143Stevan Little E<lt>stevan@iinteractive.comE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
147Copyright 2006 by Infinity Interactive, Inc.
148
149L<http://www.iinteractive.com>
150
151This library is free software; you can redistribute it and/or modify
152it under the same terms as Perl itself.
153
154=cut