Although Perl 5 doesn't use the words "class", "method" or "object" (or any of the other common OO words), it never the less has a very good OO model indeed - here's the source code of an example, together with the result of running the sample. I'll then give you a few clues!
use train;
use bus;
push (@ptl, new train("07:17",2,75));
push (@ptl, new train("06:40",3,60));
push (@ptl, new bus("07:12",2,40));
push (@ptl, new bus("08:10",1,61));
for $tranbit (@ptl) {
print $tranbit;
print " ",$tranbit->getstaff();
print " ",$tranbit->getcapacity();
print " ",$tranbit->getwhat();
print "\n";
}
__END__
The stuff below is comments as held in this
file. Split it out to separate files if you
want to run it!
############### train.pm
package train;
use pt;
@ISA = ("pt");
sub getstaff {
return 1;
}
sub getwhat {
return ("Metal wheeler");
}
1;
############### bus.pm
package bus;
use pt;
@ISA = ("pt");
sub getstaff {
my ($self) = @_;
return $$self{nv};
}
sub getwhat {
return ("Rubber wheeler");
}
1;
####################### pt.pm
package pt;
sub new {
my($class,$time,$nv,$vc)=@_;
my %info;
$info{time} = $time;
$info{nv} = $nv;
$info{vc} = $vc;
bless \%info,$class;
}
sub getcapacity {
my ($self) = @_;
return $$self{nv}*$$self{vc};
}
1;
################## Runs as ...
j8pl grahamellis$ perl tharness
train=HASH(0x801c70) 1 150 Metal wheeler
train=HASH(0x815f60) 1 180 Metal wheeler
bus=HASH(0x815f9c) 2 80 Rubber wheeler
bus=HASH(0x815ff0) 1 61 Rubber wheeler
j8pl grahamellis$
Right. Now ...
A Perl
package is a
class
A Perl
sub is used to define a
method
The list
@ISA defines inheritance
Perl's
bless function defines the container for
variables in the object
my defines a locally scoped variable
-> is used to invoke a method on an object
(written 2008-06-03)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P218 - Perl - More Objects [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[592] NOT Gone phishing - (2006-02-05)
[656] Think about your design even if you don't use full UML - (2006-03-24)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[930] -> , >= and => in Perl - (2006-11-18)
[1217] What are factory and singleton classes? - (2007-06-04)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1665] Factory method example - Perl - (2008-06-04)
[1819] Calling base class constructors - (2008-10-03)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2811] Igloos melt in the summer, but houses do not - (2010-06-15)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
[3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
[3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
[3941] Building an object based on another object in Perl - (2012-12-03)
[4096] Perl design patterns example - (2013-05-20)
[4098] Using object orientation for non-physical objects - (2013-05-22)
[4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
P213 - Perl - Creating your own Classes [975] Answering ALL the delegate's Perl questions - (2006-12-09)
[983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2169] When should I use OO techniques? - (2009-05-11)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2877] Further more advanced Perl examples - (2010-07-19)
[2969] What does blessing a variable in Perl mean? - (2010-09-24)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3833] Learning to use existing classes in Perl - (2012-08-10)
[4607] Classes and object - first steps in Perl 6 - (2016-01-02)
Some other Articles
Talk on TransWilts train service to Green PartyChecking server performance for PHP generated pagesSlow boot and terminal start on Linux boxesExample of OO in PerlPython in an afternoon - a lecture for experienced programmersWestonbirt Arboretum PostcodeEquality, sameness and identity - PythonKorn shell - some nuggetsString, Integer, Array, Associative Array - ksh variables