djs has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I would desperately like to be able to share objects
between threads. Currently I am attempting to use
Threads::Queue::Any.

$queue = Threads::Queue::Any->new; $object = MyClass->new; $queue->enqueue(\$object);

This throws an error Can't store CODE items at blib/lib/Storable.pm

Please help me. This is very annoying.

Replies are listed 'Best First'.
Re: Sharing objects in ithreads
by xdg (Monsignor) on Jan 24, 2006 at 03:14 UTC
    Can't store CODE items at blib/lib/Storable.pm

    Either your object is a blessed CODE reference, or some property of your item may be holding a code reference. There's a big caveat about that in the documentation:

    Passing unshared values between threads is accomplished by serializing the specified values using Storable when enqueuing and de-serializing the queued value on dequeuing. This allows for great flexibility at the expense of more CPU usage. It also limits what can be passed, as e.g. code references can not be serialized and therefore not be passed.

    Update: I remembered that Storable does have some support for this, so there may be a workaround:

    Since Storable version 2.05, CODE references may be serialized with the help of B::Deparse. To enable this feature, set $Storable::Deparse to a true value. To enable deserializazion, $Storable::Eval should be set to a true value. Be aware that deserialization is done through eval, which is dangerous if the Storable file contains malicious data. You can set $Storable::Eval to a subroutine reference which would be used instead of eval. See below for an example using a Safe compartment for deserialization of CODE references.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.