Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

perlobj - Perl Objects

Part:   1   2  3  4  5 

This chapter describes:

  • How to invoke subroutines as class methods.
  • How to invoke subroutines as object methods.
  • How to create objects.
  • How to create object variables.

Basic Concepts of Classes and Objects

Class - A user defined complex data type with artributes and operations.

Object - A data instance of the data type defined by a class.

Class variable - An artribute that is shared by all objects of a class. A class variable is also called a static variable.

Object variable - An artribute that is associated with each individual object of a class. An object variable is also called an instance variable.

Class method - An operation that requires a class to be implicitly associated with. A class method is also called static method.

Object method - An operation that requires an object to be implicitly associated with. An object method is also called an instance method.

In order to use classes and objects, other languages have special syntaxes to define classes, declare objects, accesse attributes, and invoke operations. But Perl tries to extend the package concept to simulate classe and reference to simulate object.

Invoking Package Subroutines as Class Methods

In Perl, any package can be used as a "class", and any subroutine in a package can be used as a class method or an object mothod.

If a subroutine is invoked as a class method, the class name (package name) will be automatically inserted into the argument list as the first argument. There are two way to invoke a subroutine as a class method:

1. Using the "indirect object" syntax:

   sub_identifier class_name arg2, arg3, ...

where "sub_identifier" is the subroutine identifier without any package name prefixes and "&"; "class_name" is the package name; and "arg2, arg3, ..." is the argument list starting from the second argument without parentheses.

2. Using the "->" notation:

   class_name->sub_identifier(arg2, arg3, ...)

where "sub_identifier" is the subroutine identifier without any package name prefixes and "&"; "class_name" is the package name; and "arg2, arg3, ..." is the argument list starting from the second argument. In this format, parentheses on the argument list are optional.

I used both syntaxes in the followiwng sample program, ClassMethodTest.pl:

#- ClassMethodTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
package Foo;
sub echoParam {
   $i = 0;
   while ( $p = shift) {
      $i++;
      print("   Param #",$i," = ",$p,"\n");
   }
}
package main;
   print("\nTest 1:\n");
   &Foo::echoParam("Jan", "Feb");
   print("\nTest 2:\n");
   Foo::echoParam("Apple", "Banana");
   print("\nTest 3:\n");
   ('Foo::echoParam')->("One", "Two");
   print("\nTest 4:\n");
   echoParam Foo "Cow", "Horse";
#   print("\nTest 5:\n");
#   &echoParam Foo "Cow", "Horse";
#   print("\nTest 6:\n");
#   echoParam Bar "Cow", "Horse";
   print("\nTest 7:\n");
   Foo->echoParam("Monday", "Tuesday");
#   print("\nTest 8:\n");
#   Bar->echoParam("Monday", "Tuesday");
   exit;

(Continued on next part...)

Part:   1   2  3  4  5 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - perlobj - Perl Objects