paurus has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,
I am new to Perl and am studying my way (slowly) through Learning Perl Objects, References, and Modules by Randal L. Schwartz with Tom Phoenix.

In Chapter 10 (Object Destruction) in the section "Additional Instance Variables in Subclasses", while attempting to compile and execute the provided code, I have found a glitch (which in my notable inexperience I cannot remedy): Here is the code:
## Additional Instance Variables in Subclasses { package Animal; sub named { my $class = shift; my $name = shift; bless \$name, $class; } sub name { my $either = shift; ref $either ? $$either : "an unamed $either"; # see above s +ection for usage } sub speak { my $either = shift; print $either->name," goes ",$either->sound,"!\n"; } sub eat { my $either = shift; my $food = shift; print $either->name," eats $food.\n"; } sub DESTROY { my $self = shift; print "[",$self->name," has died.]\n"; } } { package Horse; our @ISA = qw/Animal/; sub sound { "neigh"; } sub DESTROY { my $self = shift; $self->SUPER::DESTROY; print "[",$self->name," has gone off to the glue facto +ry.]\n"; } } { package Sheep; our @ISA = qw/Animal/; sub sound { "baaah"; } } { package Cow; our @ISA = qw/Animal/; } sub feed_a_cow_named { my $name = shift; my $cow = Cow->named($name); $cow->eat("grass"); print "Returning from the subroutine.\n"; # $cow is dest +royed here } { package Barn; sub new { bless [], shift } sub add { push @{+shift}, shift } sub contents { @{+shift} } sub DESTROY { my $self = shift; print "$self is being destroyed...\n"; for($self->contents) { print " ",$_->name," goes homeless.\n"; } } } { package Barn2; sub new { bless [], shift } sub add { push @{+shift}, shift } sub contents { @{+shift} } sub DESTROY { my $self = shift; print "$self is being destroyed...\n"; while(@$self) { my $homeless = shift(@$self); print " ",$homeless->name," goes homeless.\n" +; } } } { package RaceHorse; our @ISA = qw/Horse/; ## extend PARENT constructor sub named { my $self = shift->SUPER::named(@_); print "DEBUG: $self\n\n"; $self->{$_} = 0 for qw/wins places shows losses/; $self; } sub won { shift->{wins}++; } sub placed { shift->{places}++; } sub showed { shift->{shows}++; } sub lost { shift->{losses}++; } sub standings { my $self = shift; join ", ", map "$self->{$_} $_", qw/wins places shows +losses/; } } my $racer = RaceHorse->named("Billy Boy"); # record the outcomes: 3 wins, 1 show, 1 loss $racer->won; $racer->won; $racer->won; $racer->showed; $racer->lost; print $racer->name," has standings of: ",$racer->standings,".\n";
When executing the code, I get this warning:
Not a HASH reference at notes_chap_10.perl line 303.
which is:
$self->{$_} = 0 for qw/wins places shows losses/;
in package RaceHorse.

Why am I getting this warning? I am new to most of the material in this book, and I am looking for some clarification, so I can understand the data movement here.

Thank you in advance for your help.

Replies are listed 'Best First'.
Re: Hash ref question
by BUU (Prior) on May 11, 2004 at 03:27 UTC
    The reason you are getting this warning is that the construct: $foo->{bar}, with the arrow and the brackets, only work are things that are either hash referenecs or blessed hash references. In this case, $self is a blessed scalar, so you can't treat it as a hash reference.

    If you want to fix it, go to line 6, bless \$name, $class; And change it so that you are blessing a hash reference.
Re: Hash ref question
by japhy (Canon) on May 11, 2004 at 03:28 UTC
    The problem is that RaceHorse is expecting its object to be a hash reference, but you're using code (Horse and Animal) that is creating an object based on a scalar reference.

    Are you sure you're using the appropriate code examples?

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;