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

Thanks again to zentara for helping me out with my threading issue in wxPerl. One last obtsacle I'm facing is how do I pass a blessed object into a new thread? I have an example wxPerl app seen here. My last issue is passing the constructor object from Net::Twitter into a new thread. I've taken zentara's advice and created my worker threads prior to my UI creation and this works great. (My script no longer crashes on OS X 10.5 :) ) Currently my script creates a $twit object by logging into Twitter, but passing that blessed object to my worker thread is proving to be a challenge and I'm not sure what the best method is.

As you'll see I already have a shared hash called:

use threads; use threads::shared; use Thread::Queue; use Wx; my %thread_hash; share(%thread_hash); # ... # create $twit object eval { my $twit = Net::Twitter->new( { username => $username, password => $password, } ); if ( defined $twit ) { # I've tried setting it here # but it complains ... $thread_hash{'twit'} = $twit; # This works fine, and it's what I currently call # but I can't access this from the thread because # the worker thread was created way before any # of this code. $self->{twit} = $twit; } }; if ( my $err = $@ ) { return; } # ... # starting my actual thread sub StartThread { my ($self, $event) = @_; use Data::Dumper; print Dumper( %thread_hash ); $self->{queue}->enqueue('GO'); }
But trying to pass the blessed $twit object into the shared data structure doesn't work.

Any advice or examples would be grealy appreciated.
Thanks

Replies are listed 'Best First'.
Re: passing shared blessed object?
by BrowserUk (Patriarch) on Nov 24, 2009 at 04:12 UTC

    Try:

    ... $thread_hash{'twit'} = share $twit;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: passing shared blessed object?
by GrandFather (Saint) on Nov 24, 2009 at 02:28 UTC

    For what value of "doesn't work"? What is the issue that you see?


    True laziness is hard work
      I get an error telling me that I can't pass the object to a scalar

        Sigh! What is the text of the error? For strong preference copy and paste the actual error message into your node.


        True laziness is hard work