Hi all,
I have a need to do this (abstracted from the attached working code):
package BaseClass;
use threads;
sub doThis
{
my $this = shift;
threads->create(\&doThat,$this);
}
package DerivedClass;
use base 'BaseClass';
sub new
{
my $class = shift;
my $this = $class->SUPER::new;
return bless $this,$class;
}
sub doThat
{
...
}
--- in an app that uses these objects ---
use DerivedClass;
my $o = DerivedClass->new;
$o->doThis;
The error is:
Undefined subroutine &BaseClass::doThat called at BaseClass.pm
Notice that doThat() is not in the base class, but rather in the
subclass. Apparently Perl is not able to determine which package to use
for doThat() within doThis(). I have tried:
threads->create(\&{$this::doThat},$this);
and a few other permutations, but they are all wrong. I'm quite sure
it's matter of syntax.
I will try to explain myself again if you are confused.
Please advise. Thanks.
use strict;
use warnings;
use threads;
use threads::shared;
$|=1;
Worker->new(id=>1)->start;
while (1)
{
sleep 1;
}
# ********************************************************************
# BaseThreadedWorker class
# ********************************************************************
package BaseThreadedWorker;
use strict;
use warnings;
use threads;
use threads::shared;
sub new ($)
{
my ($class) = @_;
my %hash : shared = (
_status=>'STOPPED',
);
return bless \%hash,$class;
}
sub start
{
my $this = shift;
return threads->new(\&onStart,$this);
}
sub status
{
my ($this,$value) = @_;
lock $this;
return $this->{status} if (!defined($value));
$this->{status} = $value;
}
# ********************************************************************
# Worker class
# ********************************************************************
package Worker;
use strict;
use warnings;
use base 'BaseThreadedWorker';
sub new
{
my ($class,%option) = @_;
my $this = $class->SUPER::new;
$this->{logger} = $option{logger} if ($option{logger});
bless $this,$class;
return $this;
}
sub onStart
{
my $this = shift;
$this->status('STARTING');
eval
{
while (1)
{
foreach my $status (qw(EATING PLAYING HIDING SLEEPING
WORKING))
{
{
lock $this;
$this->status($status);
}
sleep(2);
if (!int(rand(5)))
{
die "ARGH!";
}
}
}
};
$this->status('STOPPED');
}
1;_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs