Invoking Package Subroutines as Object Methods

This section provides a tutorial example on how to invoke a package subroutine as an object method. $object->subroutine(...) is most commonly used syntax of calling object methods.

In the previous section, we learned how to convert a hard reference to an object of a specific package. Now let's see how to invoke package subroutines as object methods.

Similar to class method invocation, if a subroutine is invoked as an object method, the object (blessed reference) will be automatically inserted into the argument list as the first argument. There are two ways to invoke a subroutine as an object method:

1. Using the "indirect object" syntax:

   sub_identifier $object arg2, arg3, ...

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

2. Using the "->" notation:

   $object->sub_identifier(arg2, arg3, ...)

where "sub_identifier" is the subroutine identifier without any package name prefixes and "&"; "$object" is a blessed object; 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, ObjectMethodTest.pl:

#- ObjectMethodTest.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;
   $m = "Hello world!";
   $r = \$m;
   $x = bless($r,Foo);
   print("\nCheck data types:\n");
   print("   Type of \$n: ",ref($n),"\n");
   print("   Type of \$r: ",ref($r),"\n");
   print("   Type of \$x: ",ref($x),"\n");
   print("   \$x == \$r\n") if ($x==$r);
   print("\nTest 1:\n");
   echoParam $x "Fire", "Water";
#   print("\nTest 2:\n");
#   &echoParam $x "Fire", "Water";
#   print("\nTest 3:\n");
#   echoParam $m "Fire", "Water";
   print("\nTest 4:\n");
   $x->echoParam("Java", "Perl");
   exit;

Here is the output of the tutorial program:

Check data types:
   Type of $n:
   Type of $r: Foo
   Type of $x: Foo
   $x == $r

Test 1:
   Param #1 = Foo=SCALAR(0x1ab2f54)
   Param #2 = Fire
   Param #3 = Water

Test 4:
   Param #1 = Foo=SCALAR(0x1ab2f54)
   Param #2 = Java
   Param #3 = Perl

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