Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Perl for Larger Projects - Object Oriented Perl

Perl is a powerful language for short utility scripts - AND a powerful object oriented language too, which is great if you're going to be writing longer applications and / or a suite of programs with shared code.

We run a general course that introduced Perl for everyone and also a more advanced course for those who will be using Perl for larger projects. And one of the important aspects of that latter course is to cover how you use objects and write your own classes in Perl.

If you've never written your own class before, even the concept can be a bit daunting ... and most of the examples that you'll see will be less than short - after all, OO programming is at its best on the larger applications. So that make it quite a challenge for me to teach, given that I don't have the luxury of the longer time scale that you would have as you're actually writing a larger project. So ... here is - perhaps - one of the shortest demonstrations of a class definition and use in Perl:

package animal; # Define a class
# This is a demo! Would usually be in a separate
# file so that it could be brought in to a whole
# lot of different programs!
 
sub new {
  my ($type,$breed,$weight,$cpk) = @_;
  my %beast;
  $beast{breed} = $breed;
  $beast{weight} = $weight;
  $beast{cpk} = $cpk;
  bless \%beast;
  }
 
sub getvalue{
  my %beast = %{$_[0]};
  return $beast{weight} * $beast{cpk} / 2.2;
  }
}
 
$ermintrude = new animal("cow",2000,4.75);
# What's happening inside is:
$pinky = animal::new("animal","pig",600,1.70);
 
$val_erm = $ermintrude->getvalue();
# That's the equivalent of
$val_pink = animal::getvalue($pinky);
# BUT the first syntax only works if Perl knows what
# type of object is in $ermintude via a "bless" call.
 
print "They are worth $val_erm and $val_pink\n";


The idea of an object is that we can have a variable containing not only a number or a string, but also a piece of data that we define ourselves - in this case an "animal". And then we can define the operations that can be performed on an animal - our simplest case just allows creation and a "getvalue" operation which tells us how much the animal is worth. When you think about it, you've probably used exactly the same principle in the past with a file handle - which can be created, then read, written and closed.

The actual definition of the object is in a "package" of the same name as the object type (an object type is called a class), and as you'll usually be using each type of object you create in many different programs, it's usual to put it in a file on its own and bring it in with a use. Also because you design objects once and use them many times, the class definition many appear a bit complicated (certainly beyond the scope of today's short article) but the use should be trivially simple - thus
$ermintrude = new animal("cow",2000,4.75);
says "create me an animal in the variable called $ermintrude" and
$val_erm = $ermintrude->getvalue();
says "get me the value of the object held in $ermintrude and save it to $val_erm.



(written 2007-08-25, updated 2007-08-28)

 
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)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1664] Example of OO in Perl - (2008-06-03)
  [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)


Back to
Customer feedback - lifeblood of a business
Previous and next
or
Horse's mouth home
Forward to
Resetting session based tests in PHP
Some other Articles
Well House Manor appoints a General Manager
Easy handling of errors in PHP
Flash - is it available to your web page?
Resetting session based tests in PHP
Perl for Larger Projects - Object Oriented Perl
Customer feedback - lifeblood of a business
Well House Manor - feature comparison against the old place!
2008 course schedule - Perl, Python, PHP, Linux, Java Deployment, Ruby and more
Filtering and altering Perl lists with grep and map
Two years of campaigning for a train service
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., 2024: 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/1320_Per ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb