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

New to threads, and really new to the deeper parts of perl, been using it for years as a all purpose scripting solution, but just now getting into embedding it and using it for rapid prototyping. Basic outline of program, I have a controller which will be pretty much the heart of the program, setting things up, and several classes, one in particular that we'll call Downloader. I'm trying to make this multithreaded, so that I can have multiple downloaders going at once, but Downloader being a class instance is throwing me off. It may just be syntax, but my googling didn't produce any examples of doing threading with a class instance.
my $downloader = eval { new Downloader();} or die (@_); my $thread1 = threads->new($downloader->fetch, "http://www.webaddress.com", \%siteHash); $thread1->join;
I've tried putting \& in front of downloader->fetch, as well as putting it in front of fetch itself, but no luck. Right now its just executing the fetch function in downloader and generating an error : thread failed to start: Undefined subroutine &main::http://www.webaddress.com called Any help appreciated.

Replies are listed 'Best First'.
Re: Object Functions and Threading
by ikegami (Patriarch) on Jan 30, 2006 at 19:20 UTC
    You can't get a reference to a call. But you can wrap the call into a small function:
    my $downloader = eval { new Downloader() } or die($@); my $thread1 = threads->new( sub { $downloader->fetch("http://www.webaddress.com", \%siteHash); } ); $thread1->join;

    The (anonymous) sub will close over $downloader and %siteHash (assuming they are both lexical (my) variables), so they will be accessible to the sub even if they go out of scope.

    By the way, I changed die(@_) to die($@). Check perlvar if you're not sure which to use.

      Excellent, thanks.

        Be aware. Doing this is deceptive.

        my $downloader = eval { new Downloader() } or die($@); print $downloader; ## This will be a different instance my $thread1 = threads->new( sub { print $downloader; ### to this one $downloader->fetch("http://www.webaddress.com", \%siteHash); ## +<< This ## Will be a different hash to any similarly named hash above ## unless it was shared. } ); ## Unless you are doing other stuff here, ## this is just an exspensive synchronous call :) $thread1->join;

        That would be better coded as

        my %siteHash : shared; my $thread = threads->new( sub { my( $address, $hashref ) = @_; my $downloader = eval{ Downloader->new } or die $@; $downloader->fetch( $address, $hashref ); }, $address, \%siteHash ) or die $!; ## do other stuff $thread->join;

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Object Functions and Threading
by renodino (Curate) on Jan 30, 2006 at 20:14 UTC
    Its still not quite CPAN ready yet (its getting closer), but I'll offer Thread::Apartment as a possible solution, since (at first glance) what you're doing looks a lot like apartment threading.