Hello, all -- i have come across an interesting problem.  When i run the
following script, i get that '$this' in dog::bark is undefined.  When i
think about it, this makes sense, since call $a_dog->{'speak'}() is like
saying "dog::bark".

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'}();
************************
          _
         / \      _-'
       _/|  \-''- _ /
  __-' { |          \
      /              \
      /       "o.  |o }
      |            \ ;
                    ',
         \_         __\
           ''-_    \.//
             / '-____'
            /
          _'
        _-

************************

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

Reply via email to