in reply to Object Functions and Threading

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.

Replies are listed 'Best First'.
Re^2: Object Functions and Threading
by ramblinpeck (Sexton) on Jan 30, 2006 at 19:26 UTC
    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.