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: