On Mon, 30 Jul 2001, Mooney Christophe-CMOONEY1 wrote:

> Saying
>       $a_dog->{'speak'}($a_dog)
> on the last line would solve this problem, but it would be nice not to have
> to worry about passing itself to the function.  Does anyone see any
> alternatives to this?
>
> Thanks!
>
> #!/local/perl/bin/perl -w
> use strict;
>
> package dog;
> sub new
> {
>       my $class=shift;
>       my %this=@_;
>       $this{'speak'}=\&bark;
>       bless \%this;
> }
>
> sub bark
> {
>       my $this=shift;
>       $this || die "foo: it's undefined!\n";
>       print "$this->{'name'} says woof!\n";
> }
>
> package main;
>
> my $a_dog=dog->new(name => 'bart');
> $a_dog->{'speak'}();

My question is: why are you creating a code reference to a method that is
in the same package?  Since bark is already part of the blessed class
($this is passed in implicitly as the first argument), why are you storing
this method in the class's hash?  You aren't calling the coderef as a
method to your class, so it has no idea what the class is, which is why
you need to explicitly give it the object's reference.

-- Brett



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to