2 package # hide the package from PAUSE
3 InsideOutClass::Attribute;
11 use Scalar::Util 'refaddr';
13 use base 'Class::MOP::Attribute';
15 sub initialize_instance_slot {
16 my ($self, $meta_instance, $instance, $params) = @_;
17 my $init_arg = $self->{init_arg};
18 # try to fetch the init arg from the %params ...
20 $val = $params->{$init_arg} if exists $params->{$init_arg};
21 # if nothing was in the %params, we can use the
22 # attribute's default value (if it has one)
23 if (!defined $val && defined $self->{default}) {
24 $val = $self->default($instance);
26 $self->associated_class
28 ->set_slot_value($instance, $self->name, $val);
31 ## Method generation helpers
33 sub generate_accessor_method {
35 my $meta_class = $self->associated_class;
36 my $attr_name = $self->name;
38 my $meta_instance = $meta_class->get_meta_instance;
39 $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
40 $meta_instance->get_slot_value($_[0], $attr_name);
44 sub generate_reader_method {
46 my $meta_class = $self->associated_class;
47 my $attr_name = $self->name;
49 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
50 $meta_class->get_meta_instance
51 ->get_slot_value($_[0], $attr_name);
55 sub generate_writer_method {
57 my $meta_class = $self->associated_class;
58 my $attr_name = $self->name;
60 $meta_class->get_meta_instance
61 ->set_slot_value($_[0], $attr_name, $_[1]);
65 sub generate_predicate_method {
67 my $meta_class = $self->associated_class;
68 my $attr_name = $self->name;
70 defined $meta_class->get_meta_instance
71 ->get_slot_value($_[0], $attr_name) ? 1 : 0;
75 package # hide the package from PAUSE
76 InsideOutClass::Instance;
81 our $VERSION = '0.01';
84 use Scalar::Util 'refaddr';
86 use base 'Class::MOP::Instance';
89 my ($self, $class) = @_;
90 $self->bless_instance_structure(\(my $instance));
94 my ($self, $instance, $slot_name) = @_;
95 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance};
99 my ($self, $instance, $slot_name, $value) = @_;
100 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = $value;
103 sub initialize_slot {
104 my ($self, $instance, $slot_name) = @_;
105 $self->{meta}->add_package_variable(('%' . $slot_name) => {})
106 unless $self->{meta}->has_package_variable('%' . $slot_name);
107 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = undef;
110 sub is_slot_initialized {
111 my ($self, $instance, $slot_name) = @_;
112 return 0 unless $self->{meta}->has_package_variable('%' . $slot_name);
113 return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
124 InsideOutClass - A set of example metaclasses which implement the Inside-Out technique
131 ':attribute_metaclass' => 'InsideOutClass::Attribute',
132 ':instance_metaclass' => 'InsideOutClass::Instance'
135 __PACKAGE__->meta->add_attribute('foo' => (
142 $class->meta->new_object(@_);
145 # now you can just use the class as normal
149 This is a set of example metaclasses which implement the Inside-Out
150 class technique. What follows is a brief explaination of the code
151 found in this module.
153 We must create a subclass of B<Class::MOP::Instance> and override
154 the slot operations. This requires
155 overloading C<get_slot_value>, C<set_slot_value>, C<slot_initialized>, and
156 C<initialize_slot>, as well as their inline counterparts. Additionally we
157 overload C<add_slot> in order to initialize the global hash containing the
160 And that is pretty much all. Of course I am ignoring need for
161 inside-out objects to be C<DESTROY>-ed, and some other details as
162 well (threading, etc), but this is an example. A real implementation is left as
163 an exercise to the reader.
167 Stevan Little E<lt>stevan@iinteractive.comE<gt>
169 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
171 =head1 COPYRIGHT AND LICENSE
173 Copyright 2006 by Infinity Interactive, Inc.
175 L<http://www.iinteractive.com>
177 This library is free software; you can redistribute it and/or modify
178 it under the same terms as Perl itself.