Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Learning to use existing classes in Perl

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)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P213 - Perl - Creating your own Classes
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [246] When to bless a Perl variable - (2005-03-15)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
  [1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1664] Example of OO in Perl - (2008-06-03)
  [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)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [4607] Classes and object - first steps in Perl 6 - (2016-01-02)

P209 - Subroutines in Perl
  [96] Variable Scope - (2004-10-22)
  [308] Call by name v call by value - (2005-05-11)
  [357] Where do Perl modules load from - (2005-06-24)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [775] Do not duplicate your code - (2006-06-23)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1921] Romeo and Julie - (2008-12-04)
  [2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)


Back to
A busy day at Well House Manor - so come in and make it even busier!
Previous and next
or
Horse's mouth home
Forward to
Geekmas 2012 - celebrating open source languages such as Perl, PHP and Python
Some other Articles
Evening behind Melksham Spa
In the garden at Well House Manor
The Information age - not yet truly with us?
Geekmas 2012 - celebrating open source languages such as Perl, PHP and Python
Learning to use existing classes in Perl
A 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 Perl
Training courses - rest of 2012, 2013 and January 2014
Pimms and Croquet in Edwardian Melksham - 8th September 2012 - Food Festival Event
4759 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, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/3833_Lea ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb