3 package # hide the package from PAUSE
4 InsideOutClass::Instance;
12 use Scalar::Util 'refaddr';
14 use base 'Class::MOP::Instance';
17 my ( $self, $class ) = @_;
19 bless \$x, $class || $self->{meta}->name;
23 my ( $self, $slot_name ) = @_;
24 $self->{containers}{$slot_name} = do {
25 my $fqn = $self->{meta}->name . "::" . $slot_name;
29 $self->SUPER::add_slot( $slot_name );
33 my ( $self, $instance, $slot_name ) = @_;
34 confess "$self is no instance" unless ref $self;
35 $self->{containers}{$slot_name}{refaddr $instance};
39 my ( $self, $instance, $slot_name, $value ) = @_;
40 $self->{containers}{$slot_name}{refaddr $instance} = $value;
43 sub initialize_slot { }
45 sub slot_initialized {
46 my ( $self, $instance, $slot_name ) = @_;
47 exists $self->{containers}{$slot_name}{refaddr $instance};
50 ## &remove_slot is left as an exercise for the reader :)
60 InsideOutClass - A set of example metaclasses which implement the Inside-Out technique
66 use metaclass 'Class::MOP::Class' => (
67 # tell our metaclass to use the
68 # InsideOut attribute metclass
69 # to construct all it's attributes
70 ':instance_metaclass' => 'InsideOutClass::Instance'
73 __PACKAGE__->meta->add_attribute('foo' => (
80 $class->meta->new_object(@_);
83 # now you can just use the class as normal
87 This is a set of example metaclasses which implement the Inside-Out
88 class technique. What follows is a brief explaination of the code
91 We must create a subclass of B<Class::MOP::Instance> and override
92 the slot operations. This requires
93 overloading C<get_slot_value>, C<set_slot_value>, C<slot_initialized>, and
94 C<initialize_slot>, as well as their inline counterparts. Additionally we
95 overload C<add_slot> in order to initialize the global hash containing the
98 And that is pretty much all. Of course I am ignoring need for
99 inside-out objects to be C<DESTROY>-ed, and some other details as
100 well (threading, etc), but this is an example. A real implementation is left as
101 an exercise to the reader.
105 Stevan Little E<lt>stevan@iinteractive.comE<gt>
109 L<Tie::RefHash::Weak>
111 =head1 COPYRIGHT AND LICENSE
113 Copyright 2006 by Infinity Interactive, Inc.
115 L<http://www.iinteractive.com>
117 This library is free software; you can redistribute it and/or modify
118 it under the same terms as Perl itself.