Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Invoking Package Subroutines as Class Methods

This section provides a tutorial example on how to invoke a package subroutine as a class method. class->subroutine(...) is most commonly used syntax of calling 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 method.

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 ways 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 following tutorial program, ClassMethodTest.pl:

#- ClassMethodTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
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;

Here is the output of the tutorial program:

Test 1:
   Param #1 = Jan
   Param #2 = Feb

Test 2:
   Param #1 = Apple
   Param #2 = Banana

Test 3:
   Param #1 = One
   Param #2 = Two

Test 4:
   Param #1 = Foo
   Param #2 = Cow
   Param #3 = Horse

Test 7:
   Param #1 = Foo
   Param #2 = Monday
   Param #3 = Tuesday

Note that:

  • Tests 1, 2 and 3 are normal ways to invoke a subroutine in a package.
  • Tests 4 and 7 are special ways to invoke a subroutine as a class method.
  • Test 5 is wrong, because "&" is not allowed.
  • Tests 6 and 8 are wrong, because "Bar" is not recognized as a class (package).

Sections in This Chapter

Basic Concepts of Classes and Objects

Invoking Package Subroutines as Class Methods

bless() - Converting References to Objects

Invoking Package Subroutines as Object Methods

Class Variables and Object Variables

new() Method - Creating Objects by the Class

CalendarClass.pm - A Perl Class Example

Dr. Herong Yang, updated in 2008
Invoking Package Subroutines as Class Methods