I wrote a module that was designed to be instantiated and that object
be used by multiple threads. I guess I didn't exactly plan this out
from the beginning and ran into problems when trying to set it up so
that there was only one copy of the object and its data structure in
memory, while multiple threads called its subs.

Seeing as I couldn't just create an object in memory and share a
REFERENCE to it (this was my original plan, but that's apparently not
how threading works), I set about making it so the class, when
instantiated, existed in shared memory. I'm using threads::shared, by
the way.

My problem is that creating the object in the new() subroutine is
tedious, making sure every single bit of the data structure (most of
which is passed in as arguments) is &share()d. This gets especially
tiresome when the user passes a multi-dimensional hash reference as a
constructor argument.

My question is whether there is a simpler way for creating an packaged-
based blessed object so that it and ALL its data structures exist in
shared memory?

Sample package with constructor:

package Foo;

sub new {
    my $class = shift;
    bless {
        'option1'    => 80,
        'mydata'    => {},
        # over-ride with passed-in named arguments
        @_,
    }, $class
}

__END__

Called with:

my $f = Foo->new(
    'option1' => 20,
    'mydata' => {
        [ "one", "two" ],
        [ "three", "four" ],
    }
);

__END__

I tried it with "bless &share({ ... }), $class" but that returns an
empty blessed object.


-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to