subroutines intermediate

How to call a subroutine as a method in Perl?

Question

How to call a subroutine as a method in Perl?

In Perl, calling a subroutine as a method is a fundamental concept in object-oriented programming (OOP). While a subroutine is simply a named block of code, a method is a subroutine invoked on a class or an object (a blessed reference), allowing it to access the package's namespace and behave according to the invocant (the first parameter).

Understanding Subroutines vs. Methods

Perl does not distinguish syntax-wise between subroutines and methods. The key difference is how you call them:

  • Subroutine call: my_subroutine()
  • Method call: $object->method() or Class->method()

When called as a method, Perl implicitly passes the invocant—either the class name (string) or the blessed reference—as the first argument ($_[0]). This allows the method to operate on data tied to that invocant.

Calling a Subroutine as a Method

Any subroutine defined in a package can be called as a method by prefixing the call with an object or class name. Inside the subroutine, you usually retrieve the invocant from the first argument to access object data or class-level information.

Example: Defining and Calling a Method

use strict;
use warnings;

# Define a package (class)
package MyClass;

# Constructor method
sub new {
    my ($class, %args) = @_;
    # Bless a hash reference into class
    return bless \%args, $class;
}

# A method (subroutine called as method)
sub greet {
    my ($self, $name) = @_;   # $self is the invocant (object reference)
    print "Hello, $name! This is an object of class ", ref($self), ".\n";
}

package main;

# Create an object of MyClass
my $obj = MyClass->new(color => 'blue');

# Call the 'greet' subroutine as a method on the object
$obj->greet('Alice');

# Call the same 'greet' subroutine as a class method (invocant is class name)
MyClass->greet('Bob');

# Calling as a subroutine directly (not method), pass object explicitly
MyClass::greet($obj, 'Charlie');

Explanation of Key Concepts

  • $self in greet receives the invocant from the method call.
  • Calling $obj->greet('Alice') implicitly passes $obj as the first argument.
  • You can call the method on the class itself (MyClass->greet), where the invocant is the class name string.
  • You can also call the subroutine directly as MyClass::greet($obj, 'Charlie'), passing the object explicitly.

Perl-Specific Detail: Sigils and Context

Note that methods typically receive the invocant as the first argument. This is different from regular subroutines that are unaware of objects or classes. Perl's "There's more than one way to do it" (TMTOWTDI) philosophy allows you to call methods from classes or objects interchangeably, as long as you follow the invocant convention.

Common Pitfalls

  • Forgetting to retrieve the invocant (my ($self) = @_;) inside methods.
  • Calling a method as a subroutine without passing the invocant explicitly can cause unexpected behaviors.
  • Confusing package functions with methods—methods expect an invocant argument.

Summary

In Perl, calling a subroutine as a method simply means invoking it on an object or class. The invocant is passed as the first argument and allows the subroutine to act as a method that operates on object or class data. By following this convention, you can easily write and use object-oriented code in Perl.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 6ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Hello, Alice! This is an object of class MyClass.
Hello, Bob! This is an object of class .
Hello, Charlie! This is an object of class MyClass.
STDERR
(empty)

Was this helpful?

Related Questions