Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Example of OO in Perl

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 17:32:36)

 
Associated topics are indexed under
P213 - Perl - Creating your own Classes
P218 - Perl - More Objects

Back to
Python in an afternoon - a lecture for experienced programmers
Previous and next
or
Horse's mouth home
Forward to
Factory method example - Perl
Some other Articles
Talk on TransWilts train service to Green Party
Checking server performance for PHP generated pages
Slow boot and terminal start on Linux boxes
Factory method example - Perl
Example of OO in Perl
Python in an afternoon - a lecture for experienced programmers
Westonbirt Arboretum Postcode
Equality, sameness and identity - Python
Korn shell - some nuggets
String, Integer, Array, Associative Array - ksh variables
1889 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho