If you've got a whole lot of logic that you want to be able to run on data of a certain type, you'll typically put all of that logic into a grouping of its own - typically known as a
package or a
class, and you'll then call that logic at a high level from outside the package or class. This is a great approach for writing code that is reusable, each to test and easy to maintain. And it means that others (who don't have any in depth understanding of the internals of handling the data type) can easily use your logic.
For the updated
Perl Programming course that I am trialing this week, I have written a new class (and put it
[here]), and I'm showing delegates how to
make use of the class before I teach them a little bit about how to
write such classes of their own.
Here's how to make use of that class ... firstly, load in the logic from its own file:
use train;
And then you can call / make use of that logic using explicit predefined subs known as
methods:
$soton = new train(3,74);
$l1 = $soton->getlength();
$c1 = $soton->getcapacity();
$s1 = $soton->getcapacity(1.0);
print "Southampton: $l1 $c1 $s1\n";
The result of running that is
Southampton: 69 310 222
which tells us that the train is 69 metres long, has 222 seats, and a capacity of 310 passengers. You may wonder about how these figures are reached - the good news is that
as a user of the class you don't need to know the algorithms that are used - you can just call the code.
Full test program is
[here] - we've created several train objects in that program to give a little more help in training, and from a testing viewpoint we need to check that the code works with several trains of different lengths and seating arrangements - in other words that variables are different for each train and not shared. We've also provided a second test program
[here] to start showing class reuse
You'll learn how to use objects on our
Learning to program in Perl and
Perl Programming courses. If you want to learn in details how to design and create your own packages and families of classes and methods, you'll want to follow one of those two fundamental courses with our
Perl for larger projects course.
(written 2012-08-10, updated 2012-08-18)
299b
Associated topics are indexed under
P213 - Perl - Creating your own Classes [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[2969] What does blessing a variable in Perl mean? - (2010-09-24)
[2877] Further more advanced Perl examples - (2010-07-19)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2169] When should I use OO techniques? - (2009-05-11)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[1664] Example of OO in Perl - (2008-06-03)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[975] Answering ALL the delegate's Perl questions - (2006-12-09)
[246] When to bless a Perl variable - (2005-03-15)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
P209 - Subroutines in Perl [3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[1921] Romeo and Julie - (2008-12-04)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[969] Perl - $_ and @_ - (2006-12-07)
[775] Do not duplicate your code - (2006-06-23)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[531] Packages in packages in Perl - (2005-12-16)
[357] Where do Perl modules load from - (2005-06-24)
[308] Call by name v call by value - (2005-05-11)
[96] Variable Scope - (2004-10-22)
Some other Articles
Evening behind Melksham SpaIn the garden at Well House ManorThe Information age - not yet truly with us?Geekmas 2012 - celebrating open source languages such as Perl, PHP and PythonLearning to use existing classes in PerlA busy day at Well House Manor - so come in and make it even busier!Our Melksham Hotel is not quiet - but we are waiting to give you a quiet welcome. Big difference!Traversing a directory in PerlTraining courses - rest of 2012, 2013 and January 2014Pimms and Croquet in Edwardian Melksham - 8th September 2012 - Food Festival Event