CPAN::Forum
Object-InsideOut - Storing objects inside of shared objects
| Posted on Thu Oct 25 00:14:52 2007 by buchet |
| Storing objects inside of shared objects |
|
As mentioned in BUGS AND LIMITATIONS it is not possible to store objects inside of shared objects. I discovered that this not true for classical hash blessed objects stored in an Object::InsideOut container. It seems to me that the problem is the reference counter which demolish the InsideOut object inside of the container too early.
package Container;
use threads;
use threads::shared;
{
use Object::InsideOut ':SHARED';
my @objects : Field : All(objects) : Type(ARRAY_ref);
sub object {
my ( $self, $object ) = @_;
return $self->objects()->[-1] unless $object;
die 'parameter should be able to do data'
unless ( ref($object) && $object->can('data') );
$self->set( \@objects, [] )
unless ( ref $objects[$$self] eq 'ARRAY' );
push @{ $self->objects() }, $object;
return $self->objects()->[-1];
}
}
package Insideout;
use threads;
use threads::shared;
{
use Object::InsideOut ':SHARED';
my @data : Field : All(data);
}
package HashBlessed;
use threads;
use threads::shared;
sub new {
my ( $class, $args ) = @_;
my $self : shared;
$self = &share( {} );
bless $self, $class;
if ( ref $args eq 'HASH' && exists $args->{data} ) {
$self->{data} = $args->{data};
}
return $self;
}
sub data {
my ( $self, $val ) = @_;
if ( defined $val ) {
lock $self;
$self->{data} = $val;
}
return $self->{data};
}
my $io = Insideout->new( { data => 'insideout' } );
my $hb = HashBlessed->new( { data => 'hashblessed' } );
my $container = Container->new();
$container->object($hb);
warn('hash bless object has lost his data')
unless ( $container->objects()->[-1]->data() eq 'hashblessed' );
$container->object($io);
warn('inside out object has lost his data')
unless ( $container->objects()->[-1]->data() eq 'insideout' );
$container->object($hb);
warn('hash bless object has lost his data')
unless ( $container->objects()->[-1]->data() eq 'hashblessed' );
Am I on the right path?
|
| Direct Responses: 6334 | Write a response |
(2)
]