I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2007-10-24 21:14:52-07 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