perlboot problem

A

Art Werschulz

Hi.

I'm having a problem with "A horse of a different color", found towards the
end of the perlboot manpage.

Stripping things to the bone, here's my Perl file:

%<------%<--%<--%<---cut here---%<--%<--%<----------------------------
use strict vars;

{package Animal;
sub speak {
my $either = shift;
print $either->name, " goes ", $either->sound, "!\n";
}

sub new {
my $class = shift;
my $name = shift;
my $self = { Name => $name, Color => $class->defaultColor };
bless \$name, $class;
}

sub set_color {
$_[0]->{Color} = $_[1];
}

}

{package Horse;
use vars qw(@ISA);
@ISA = qw(Animal);

sub sound {"neigh"}

sub defaultColor { "brown" };
}

my $talking = Horse->new("Mr. Ed");
$talking->set_color("black-and-white");
%<------%<--%<--%<---cut here---%<--%<--%<----------------------------

The problem is that the $talking->set_color statement bombs with the error
msg "Not a HASH reference at foo line 19."

Of course, I looked for typos. I didn't see anything, but that may not
mean all that much.

I checked using Google, as well as the groups.gooogle.com archive of this
newsgroup. Of course, there's always the possibility that my search
criteria weren't good enough.

I also used perldebug. It didn't tell me anything too surprising:

%<------%<--%<--%<---cut here---%<--%<--%<----------------------------
main::(21:5): {package Animal;
main::(21:6): sub speak {
DB<1> b Animal::set_color
DB<2> c
Animal::set_color(21:36): $_[0]->{Color} = $_[1];
DB<2> x @_
0 Horse=SCALAR(0x8059ee8)
-> 'Mr. Ed'
1 'black-and-white'
DB<3> x @_[0]
0 Horse=SCALAR(0x8059ee8)
-> 'Mr. Ed'
DB<4> x @_[0]->{Color}
Not a HASH reference at (eval 5)[/usr/lib/perl5/5.8.0/perl5db.pl:17] line 2.
%<------%<--%<--%<---cut here---%<--%<--%<----------------------------

If anybody sees what I've missed here, I would greatly appreciate it. Many
thanks.
 
U

Uri Guttman

AW> I'm having a problem with "A horse of a different color", found
AW> towards the end of the perlboot manpage.

AW> Stripping things to the bone, here's my Perl file:

AW> %<------%<--%<--%<---cut here---%<--%<--%<----------------------------
AW> use strict vars;

AW> {package Animal;

you don't need that opening {. packages are file scoped or until the
next package command.

AW> sub speak {
AW> my $either = shift;
AW> print $either->name, " goes ", $either->sound, "!\n";
AW> }

AW> sub new {
AW> my $class = shift;
AW> my $name = shift;
AW> my $self = { Name => $name, Color => $class->defaultColor };
AW> bless \$name, $class;

you don't return $self but you bless the name. looks like a nasty little
bug right there.

uri
 
J

John J. Trammell

sub new {
my $class = shift;
my $name = shift;
my $self = { Name => $name, Color => $class->defaultColor };
bless \$name, $class;
}

You probably want:

bless $self, $class;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top