in reply to Inheritance in Perl

the "use" let you import into the current namespace some the semantics that are in the module you provide to it, these semantics include the methods and variables that would be available to the current namespace ..

Inheritance on the other hand comes into play when you are writing a class (derived) that would use methods from another class (base), so the derived class inherits from the base class, this would be provided in the @ISA of the derived class, so when Perl can not find a method or a class in your current package it would check the @ISA for a class that has that method and subsequently execute it..

"Does that mean log() become a method of Catalyst context and that's what we call inheritance in Perl?"

It is a method invoked by the object used in your program, which is an object of Catalyst but it doesn't necessarily need to be inherited by Catalyst or taken as a representer of what inheritance is unless Catalyst itself has inherited this particular method from another base class, check the documentation..

An example of Inheritance, I would have two classes, a base CLASS1.pm and a derived CLASS2.pm, the second class would inherit the method declared in the first one and hence calling the second class and using it would call that method from CLASS1 but as a method of CLASS2..

#!/usr/local/bin/perl CLASS1.pm package CLASS1; sub new{ my $self = {}; bless($self); return $self; } sub gettext{ #this is the method I want to inherit return "I come from CLASS1.pm\n"; }; return 1;
Inheritance is done by:
#!/usr/local/bin/perl #CLASS2.pm package CLASS2; #import into the current namespace the symbol table of CLASS1 use CLASS; @ISA=qw(CLASS1); #Inherit from Class1. sub new{ my $self = CLASS1->new; #call constructor method of CLASS1 return bless($self); }
Using CLASS2 that inherits from CLASS1:
#!/usr/local/bin/perl #TryingIt.pl use CLASS2; my $object1=CLASS2->new(); print "The object says: ",$object1->gettext(),"\n"; print "The object class is ",ref $object1, "\n"; #The object says: I come from CLASS1.pm #The object class is CLASS2

The above code is just a proof of principle that illustrates what inheritance is, I hope you find it instructive and criticism is accepted with an open heart...but the subject is a vast one...You might also be interested in checking:


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: Inheritance in Perl
by sman (Beadle) on Jan 19, 2010 at 06:18 UTC
    Thanks a lot for such insightful comments.