in reply to Inheritance in Perl5

Perl tends to like its packages defined before they're used. Try putting both your packages inside a BEGIN block. ie
my $horse1 = Horse->new("George the Horse", 12); $horse1->present(); $horse1->animalmethod(); BEGIN { package Animal; ... package Horse; ... }

Update:

I forgot I was going to point you towards Moose. It's a nicer way of writing OO Perl. Then, try MooseX::Declare for a much nicer way.

Replies are listed 'Best First'.
Re^2: Inheritance in Perl5
by chromatic (Archbishop) on Aug 08, 2009 at 18:17 UTC
    Perl tends to like its packages defined before they're used.

    Indeed, this is why the Perl 5 parser creates namespaces and installs subroutines into those name spaces at compilation time.

    Except for code which creates an object in a BEGIN block before the parser has even read the class package declaration, this order does not matter. The OP's question is similar to:

    ok( foo(), 10, 'foo() should return value of $bar' ); my $bar = 10; sub foo { return $bar }
Re^2: Inheritance in Perl5
by EvanCarroll (Chaplain) on Aug 08, 2009 at 17:46 UTC
    The moose way you speak of:
    package Animal; use Moose; has [qw/name age/] => ( isa => 'Str', is => 'rw' ); package Horse; use Moose; extends 'Animal'; sub present { my $self = shift; print $self->isa('Animal') ? "I am an animal" : "I am not animal" }
    With that said, I think there is something seriously wrong if you want your subclass to confirm that it inherits from the parent class in a method ;)


    Evan Carroll
    The most respected person in the whole perl community.
    www.EvanCarroll.com