In a message dated 3/28/2006 5:50:21 A.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> 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);
 
at this point, the compiler is looking for a subroutine  doThat  in the package
BaseClass and all classes (i.e., packages) that BaseClass derives from.  
however, BaseClass derives from nothing; in particular, it does not derive
from DerivedClass, where a subroutine named  doThat  (or more specifically,
&DerivedClass::doThat) is defined.   this is what the error message is saying.  
a way around the problem is to say  
 
    threads->create(\&DerivedClass::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:
 
 
perl knows exactly which package (or packages) to use because you have
told it exactly which to use.  

>
> 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.
 
hi ji-haw --  
 
consider the following:  
 
-------- begin code ----------------
 
# t_jhfoo1.pl       derived classes                                 28mar06waw
 
use strict;
use warnings;
 
package Foo;
 
sub foo {
    bar();
    }
 
sub bar{
    Bar::drunk();
    printf qq(i am bar in %s \n), __PACKAGE__;
    }
 
package Bar;
use base 'Foo';
 
sub drunk {
    printf qq(i am drunk in %s \n), __PACKAGE__;
    }
 
package main;
 
Bar->foo();  # ok: &foo() defined in Bar or in one of its base classes
Foo::foo();
# Bar::foo();  # fails: no &Bar::foo() defined in package Bar
# drunk();  # fails: no &drunk() defined in package main
 
package Bar;
 
drunk;  # ok
############################
 
package BaseClass;
use threads;
 
sub doThis
{
    my $this = shift;
 
    # threads->create(\&doThat,$this);  # no
 
    # threads->create(\&DerivedClass::doThat,$this);  # should work
 
    $this->{_code}->('YES');
 
}
 
sub new ($)
{
    my ($class) = @_;
 
    my %hash = (
        _status=>'STOPPED',
        _code  => \&DerivedClass::doThat,
        );
 
    return bless \%hash,$class;
}
 
package DerivedClass;
use base 'BaseClass';
 
sub new
{
    my $class = shift;
    my $this = $class->SUPER::new;
    return bless $this,$class;
}
 
sub doThat
{
   my ($message) = @_;
 
   print "in doThat: $message \n";
}
 
# --- in an app that uses these objects ---
 
package main;
 
# use DerivedClass;
my $o = DerivedClass->new;
$o->doThis;
 
------------ end code ----------------
 
hth -- bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to