2 package # hide the package from PAUSE
3 InsideOutClass::Instance;
11 use Scalar::Util 'refaddr';
13 use base 'Class::MOP::Instance';
16 my ($self, $class) = @_;
17 $self->bless_instance_structure(\(my $instance));
21 my ($self, $instance, $slot_name) = @_;
22 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance};
26 my ($self, $instance, $slot_name, $value) = @_;
27 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = $value;
31 my ($self, $instance, $slot_name) = @_;
32 $self->{meta}->add_package_variable(('%' . $slot_name) => {})
33 unless $self->{meta}->has_package_variable('%' . $slot_name);
34 $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = undef;
37 sub is_slot_initialized {
38 my ($self, $instance, $slot_name) = @_;
39 return 0 unless $self->{meta}->has_package_variable('%' . $slot_name);
40 return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
43 sub inline_slot_access {
44 my ($self, $instance, $slot_name) = @_;
45 $slot_name =~ s/\'//g;
46 ('$' . $self->{meta}->name . '::' . $slot_name . '{Scalar::Util::refaddr(' . $instance . ')}');
57 InsideOutClass - A set of example metaclasses which implement the Inside-Out technique
64 ':instance_metaclass' => 'InsideOutClass::Instance'
67 __PACKAGE__->meta->add_attribute('foo' => (
74 $class->meta->new_object(@_);
77 # now you can just use the class as normal
81 This is a set of example metaclasses which implement the Inside-Out
82 class technique. What follows is a brief explaination of the code
85 We must create a subclass of B<Class::MOP::Instance> and override
86 the slot operations. This requires
87 overloading C<get_slot_value>, C<set_slot_value>, C<slot_initialized>, and
88 C<initialize_slot>, as well as their inline counterparts. Additionally we
89 overload C<add_slot> in order to initialize the global hash containing the
92 And that is pretty much all. Of course I am ignoring need for
93 inside-out objects to be C<DESTROY>-ed, and some other details as
94 well (threading, etc), but this is an example. A real implementation is left as
95 an exercise to the reader.
99 Stevan Little E<lt>stevan@iinteractive.comE<gt>
101 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
103 =head1 COPYRIGHT AND LICENSE
105 Copyright 2006 by Infinity Interactive, Inc.
107 L<http://www.iinteractive.com>
109 This library is free software; you can redistribute it and/or modify
110 it under the same terms as Perl itself.