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:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

Objects (or References) and Classes (or Packages)

 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

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version