1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package # hide the package from PAUSE
ClassEncapsulatedAttributes;
use strict;
use warnings;
our $VERSION = '0.06';
use parent 'Class::MOP::Class';
sub initialize {
(shift)->SUPER::initialize(@_,
# use the custom attribute metaclass here
'attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute',
);
}
sub construct_instance {
my ($class, %params) = @_;
my $meta_instance = $class->get_meta_instance;
my $instance = $meta_instance->create_instance();
# initialize *ALL* attributes, including masked ones (as opposed to applicable)
foreach my $current_class ($class->class_precedence_list()) {
my $meta = $current_class->meta;
foreach my $attr_name ($meta->get_attribute_list()) {
my $attr = $meta->get_attribute($attr_name);
$attr->initialize_instance_slot($meta_instance, $instance, \%params);
}
}
return $instance;
}
package # hide the package from PAUSE
ClassEncapsulatedAttributes::Attribute;
use strict;
use warnings;
our $VERSION = '0.04';
use parent 'Class::MOP::Attribute';
# alter the way parameters are specified
sub initialize_instance_slot {
my ($self, $meta_instance, $instance, $params) = @_;
# if the attr has an init_arg, use that, otherwise,
# use the attributes name itself as the init_arg
my $init_arg = $self->init_arg();
# try to fetch the init arg from the %params ...
my $class = $self->associated_class;
my $val;
$val = $params->{$class->name}->{$init_arg}
if exists $params->{$class->name} &&
exists ${$params->{$class->name}}{$init_arg};
# if nothing was in the %params, we can use the
# attribute's default value (if it has one)
if (!defined $val && $self->has_default) {
$val = $self->default($instance);
}
# now add this to the instance structure
$meta_instance->set_slot_value($instance, $self->name, $val);
}
sub name {
my $self = shift;
return ($self->associated_class->name . '::' . $self->SUPER::name)
}
1;
__END__
=pod
=head1 NAME
ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
=head1 SYNOPSIS
package Foo;
use metaclass 'ClassEncapsulatedAttributes';
Foo->meta->add_attribute('foo' => (
accessor => 'Foo_foo',
default => 'init in FOO'
));
sub new {
my $class = shift;
$class->meta->new_object(@_);
}
package Bar;
our @ISA = ('Foo');
# duplicate the attribute name here
Bar->meta->add_attribute('foo' => (
accessor => 'Bar_foo',
default => 'init in BAR'
));
# ... later in other code ...
my $bar = Bar->new();
prints $bar->Bar_foo(); # init in BAR
prints $bar->Foo_foo(); # init in FOO
# and ...
my $bar = Bar->new(
'Foo' => { 'foo' => 'Foo::foo' },
'Bar' => { 'foo' => 'Bar::foo' }
);
prints $bar->Bar_foo(); # Foo::foo
prints $bar->Foo_foo(); # Bar::foo
=head1 DESCRIPTION
This is an example metaclass which encapsulates a class's
attributes on a per-class basis. This means that there is no
possibility of name clashes with inherited attributes. This
is similar to how C++ handles its data members.
=head1 ACKNOWLEDGEMENTS
Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
=head1 AUTHORS
Stevan Little E<lt>stevan@iinteractive.comE<gt>
Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2006-2008 by Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|