2 package # hide the package from PAUSE
3 ClassEncapsulatedAttributes;
10 our $VERSION = '0.01';
12 use base 'Class::MOP::Class';
14 sub construct_instance {
15 my ($class, %params) = @_;
17 foreach my $current_class ($class->class_precedence_list()) {
18 $instance->{$current_class} = {}
19 unless exists $instance->{$current_class};
20 my $meta = $class->initialize($current_class);
21 foreach my $attr_name ($meta->get_attribute_list()) {
22 my $attr = $meta->get_attribute($attr_name);
23 # if the attr has an init_arg, use that, otherwise,
24 # use the attributes name itself as the init_arg
25 my $init_arg = $attr->has_init_arg() ? $attr->init_arg() : $attr->name;
26 # try to fetch the init arg from the %params ...
28 $val = $params{$current_class}->{$init_arg}
29 if exists $params{$current_class} &&
30 exists ${$params{$current_class}}{$init_arg};
31 # if nothing was in the %params, we can use the
32 # attribute's default value (if it has one)
33 $val ||= $attr->default($instance) if $attr->has_default();
34 # now add this to the instance structure
35 $instance->{$current_class}->{$attr_name} = $val;
41 sub attribute_metaclass { 'ClassEncapsulatedAttributes::Attribute' }
43 package # hide the package from PAUSE
44 ClassEncapsulatedAttributes::Attribute;
49 use Class::MOP 'meta';
51 our $VERSION = '0.01';
53 use base 'Class::MOP::Attribute';
55 sub generate_accessor_method {
56 my ($self, $attr_name) = @_;
57 my $class_name = $self->associated_class->name;
59 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1] if scalar(\@_) == 2;
60 \$_[0]->{'$class_name'}->{'$attr_name'};
64 sub generate_reader_method {
65 my ($self, $attr_name) = @_;
66 my $class_name = $self->associated_class->name;
68 \$_[0]->{'$class_name'}->{'$attr_name'};
72 sub generate_writer_method {
73 my ($self, $attr_name) = @_;
74 my $class_name = $self->associated_class->name;
76 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1];
80 sub generate_predicate_method {
81 my ($self, $attr_name) = @_;
82 my $class_name = $self->associated_class->name;
84 defined \$_[0]->{'$class_name'}->{'$attr_name'} ? 1 : 0;
88 ## &remove_attribute is left as an exercise for the reader :)
98 ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
104 sub meta { ClassEncapsulatedAttributes->initialize($_[0]) }
106 Foo->meta->add_attribute('foo' => (
107 accessor => 'Foo_foo',
108 default => 'init in FOO'
113 bless $class->meta->construct_instance(@_) => $class;
119 # duplicate the attribute name here
120 Bar->meta->add_attribute('foo' => (
121 accessor => 'Bar_foo',
122 default => 'init in BAR'
125 # ... later in other code ...
127 my $bar = Bar->new();
128 prints $bar->Bar_foo(); # init in BAR
129 prints $bar->Foo_foo(); # init in FOO
134 'Foo' => { 'foo' => 'Foo::foo' },
135 'Bar' => { 'foo' => 'Bar::foo' }
138 prints $bar->Bar_foo(); # Foo::foo
139 prints $bar->Foo_foo(); # Bar::foo
143 This is an example metaclass which encapsulates a class's
144 attributes on a per-class basis. This means that there is no
145 possibility of name clashes with inherited attributes. This
146 is similar to how C++ handles its data members.
148 =head1 ACKNOWLEDGEMENTS
150 Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
154 Stevan Little E<lt>stevan@iinteractive.comE<gt>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2006 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.