Search code examples
perlinheritancemoose

Moose and extending non moose classes


I have the following class / package:

package Data::CrawlerThreadPool;
use Moose;
use MooseX::InsideOut;
use MooseX::NonMoose;

extends 'Thread::Pool::Simple';

around BUILDARGS => sub {
      my $orig  = shift;
      my $class = shift;
      return $class->$orig( do  => [\&_do_handle],
                            min => 5,
                            max => 10 );
};

sub _do_handle {
    $| = 1;
    print "In Do handle";
}
1;

in my main script, I call it (for e.g...) like this:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::CrawlerThreadPool;

my $tp = Data::CrawlerThreadPool->new();
my @args = qw(0 0 0 0 0 0 0 0 0);
$tp->add(@args) for (0..10);
$tp->join();

Seems like the BUILDARGS method is called, but the process do handle never being called. What am I missing here? Thread::Pool::Simple

thanks,


Solution

  • \&_do_handle is not calling the _do_handle sub - it's just dereferencing a reference to the sub. This idiom is normally used inside an eval {} in order to check that you really have a subref (or an object with a subref overload) -- which I don't think is what you intended to do here.

    If you are intending to actually call _do_handle, then call it directly:

    around BUILDARGS => sub {
          my $orig  = shift;
          my $class = shift;
          return $class->$orig( do  => [ _do_handle() ],
                                min => 5,
                                max => 10 );
    };