package # hide the package from PAUSE InsideOutClass::Instance; use strict; use warnings; our $VERSION = '0.06'; use Carp 'confess'; use Scalar::Util 'refaddr'; use base 'Class::MOP::Instance'; sub create_instance { my ( $self, $class ) = @_; my $x; bless \$x, $class || $self->{meta}->name; } sub add_slot { my ( $self, $slot_name ) = @_; $self->{containers}{$slot_name} = do { my $fqn = $self->{meta}->name . "::" . $slot_name; no strict 'refs'; \%$fqn; }; $self->SUPER::add_slot( $slot_name ); } sub get_slot_value { my ( $self, $instance, $slot_name ) = @_; confess "$self is no instance" unless ref $self; $self->{containers}{$slot_name}{refaddr $instance}; } sub set_slot_value { my ( $self, $instance, $slot_name, $value ) = @_; $self->{containers}{$slot_name}{refaddr $instance} = $value; } sub initialize_slot { } sub slot_initialized { my ( $self, $instance, $slot_name ) = @_; exists $self->{containers}{$slot_name}{refaddr $instance}; } ## &remove_slot is left as an exercise for the reader :) 1; __END__ =pod =head1 NAME InsideOutClass - A set of example metaclasses which implement the Inside-Out technique =head1 SYNOPSIS package Foo; use metaclass 'Class::MOP::Class' => ( # tell our metaclass to use the # InsideOut attribute metclass # to construct all it's attributes ':instance_metaclass' => 'InsideOutClass::Instance' ); __PACKAGE__->meta->add_attribute('foo' => ( reader => 'get_foo', writer => 'set_foo' )); sub new { my $class = shift; $class->meta->new_object(@_); } # now you can just use the class as normal =head1 DESCRIPTION This is a set of example metaclasses which implement the Inside-Out class technique. What follows is a brief explaination of the code found in this module. We must create a subclass of B and override the slot operations. This requires overloading C, C, C, and C, as well as their inline counterparts. Additionally we overload C in order to initialize the global hash containing the actual slot values. And that is pretty much all. Of course I am ignoring need for inside-out objects to be C-ed, and some other details as well (threading, etc), but this is an example. A real implementation is left as an exercise to the reader. =head1 AUTHOR Stevan Little Estevan@iinteractive.comE =head1 SEE ALSO L =head1 COPYRIGHT AND LICENSE Copyright 2006 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut