2 package # hide the package from PAUSE
3 ClassEncapsulatedAttributes;
10 use base 'Class::MOP::Class';
13 (shift)->SUPER::initialize(@_,
14 # use the custom attribute metaclass here
15 ':attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute'
19 sub construct_instance {
20 my ($class, %params) = @_;
22 foreach my $current_class ($class->class_precedence_list()) {
23 $instance->{$current_class} = {}
24 unless exists $instance->{$current_class};
25 my $meta = $class->initialize($current_class);
26 foreach my $attr_name ($meta->get_attribute_list()) {
27 my $attr = $meta->get_attribute($attr_name);
28 # if the attr has an init_arg, use that, otherwise,
29 # use the attributes name itself as the init_arg
30 my $init_arg = $attr->has_init_arg() ? $attr->init_arg() : $attr->name;
31 # try to fetch the init arg from the %params ...
33 $val = $params{$current_class}->{$init_arg}
34 if exists $params{$current_class} &&
35 exists ${$params{$current_class}}{$init_arg};
36 # if nothing was in the %params, we can use the
37 # attribute's default value (if it has one)
38 $val ||= $attr->default($instance) if $attr->has_default();
39 # now add this to the instance structure
40 $instance->{$current_class}->{$attr_name} = $val;
46 package # hide the package from PAUSE
47 ClassEncapsulatedAttributes::Attribute;
52 our $VERSION = '0.02';
54 use base 'Class::MOP::Attribute';
56 sub generate_accessor_method {
57 my ($self, $attr_name) = @_;
58 my $class_name = $self->associated_class->name;
60 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1] if scalar(\@_) == 2;
61 \$_[0]->{'$class_name'}->{'$attr_name'};
65 sub generate_reader_method {
66 my ($self, $attr_name) = @_;
67 my $class_name = $self->associated_class->name;
69 \$_[0]->{'$class_name'}->{'$attr_name'};
73 sub generate_writer_method {
74 my ($self, $attr_name) = @_;
75 my $class_name = $self->associated_class->name;
77 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1];
81 sub generate_predicate_method {
82 my ($self, $attr_name) = @_;
83 my $class_name = $self->associated_class->name;
85 defined \$_[0]->{'$class_name'}->{'$attr_name'} ? 1 : 0;
89 ## &remove_attribute is left as an exercise for the reader :)
99 ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
105 sub meta { ClassEncapsulatedAttributes->initialize($_[0]) }
107 Foo->meta->add_attribute('foo' => (
108 accessor => 'Foo_foo',
109 default => 'init in FOO'
114 bless $class->meta->construct_instance(@_) => $class;
120 # duplicate the attribute name here
121 Bar->meta->add_attribute('foo' => (
122 accessor => 'Bar_foo',
123 default => 'init in BAR'
126 # ... later in other code ...
128 my $bar = Bar->new();
129 prints $bar->Bar_foo(); # init in BAR
130 prints $bar->Foo_foo(); # init in FOO
135 'Foo' => { 'foo' => 'Foo::foo' },
136 'Bar' => { 'foo' => 'Bar::foo' }
139 prints $bar->Bar_foo(); # Foo::foo
140 prints $bar->Foo_foo(); # Bar::foo
144 This is an example metaclass which encapsulates a class's
145 attributes on a per-class basis. This means that there is no
146 possibility of name clashes with inherited attributes. This
147 is similar to how C++ handles its data members.
149 =head1 ACKNOWLEDGEMENTS
151 Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
155 Stevan Little E<lt>stevan@iinteractive.comE<gt>
157 =head1 COPYRIGHT AND LICENSE
159 Copyright 2006 by Infinity Interactive, Inc.
161 L<http://www.iinteractive.com>
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself.