Main Content

Perl for Larger Projects - Object Oriented Perl

Archive - Originally posted on "The Horse's Mouth" - 2007-08-25 15:16:00 - Graham Ellis

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.