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

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:

  • It is true that you can bless any reference into an object. In my sample program, I blessed a reference of the scalar into an object of class "Foo".
  • Tests 1 and 4 are special ways to invoke a subroutine as an object method.
  • Test 2 is wrong, because "&" is not allowed.
  • Test 3 is wrong, because "$m" is not an object.

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 Object Methods