Front page | perl.beginners |
Postings from May 2012
Unblessed reference problem
Thread Next
From:
sono-io
Date:
May 19, 2012 20:09
Subject:
Unblessed reference problem
Message ID:
1F0CD4DF-B755-4B3B-9383-9FCC4686629D@fannullone.us
I'm trying the following code from Programming Perl, but I'm get the error "Can't call method "type" on unblessed reference at untitled text 10 line 23". I've checked the errata page, but there's nothing listed for it. Searching on the error message brings up pages about unblessed references, but I can't make heads or tails out of them. Does anyone know what the problem is?
Thanks,
Marc
use v5.14;
use strict;
use warnings;
package Stables 1.01 {
use Moose;
has "animals" => (
traits => ["Array"],
is => "rw",
isa => "ArrayRef[Animal]",
default => sub { [] },
handles => {
add_animal => "push",
add_animals => "push",
},
);
sub roll_call {
my($self) = @_;
for my $animal($self->animals) {
say "Some ", $animal->type,
" named ", $animal->name,
" is in the stable";
}
}
}
package Animal 1.01 {
use Moose;
has "name" => (
is => "rw",
isa => "Str",
required => 1,
);
has "type" => (
is => "rw",
isa => "Str",
default => "animal",
);
}
my $stables = Stables->new;
$stables->add_animal(
Animal->new(name => "Mr. Ed", type => "horse")
);
$stables->add_animals(
Animal->new(name => "Donkey", type => "donkey"),
Animal->new(name => "Lampwick", type => "donkey"),
Animal->new(name => "Trigger", type => "horse" ),
);
$stables->roll_call;
Thread Next
-
Unblessed reference problem
by sono-io