Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
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 15:16:00)

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

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
2259 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 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., 2009: 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