3.4 Arrow Notation (->)

Object-oriented Perl code uses arrow notation (->) to call methods. Understanding how this works is essential to understanding OO Perl. Before you start reading OO Perl code, let's look more closely at its main features and how arrow notation is used to call methods.[2]

[2] The arrow (->) also appears in Perl when dealing with complex data structures, as you saw in Chapter 2, where it's used for references to subroutines.

The arrow notation is used on an object to call a method in a class. Because the object has been blessed (i.e., marked with the class name), Perl can tell from the object what class it's in and so knows to find the method in that same class. With arrow notation, Perl also passes to the method a new argument that automatically appears first in its argument list. The other arguments are shifted over; the first argument is now the second, and so on. The automatic passing of a new first argument to the method is the key to understanding OO Perl code.

The method name appears to the right of the arrow. Perl then uses what's immediately to the left of the arrow to identify the class in which to find the method. It also passes information about what's on the left of the arrow to the method, where it appears as the first argument of the method. The left side of the arrow may be in one of two forms:

  • The name of the class. Here's an example:

    TRNA->new(  );

    Here Perl sees that the left side is the name of the TRNA class; therefore it calls the new subroutine in the TRNA package. It also automatically passes the name TRNA to that subroutine as its first argument, shifting any other arguments that may have been explicitly given (you'll see how this feature is used in later examples). You need to save the new object (a blessed reference to a hash) using the assignment operator = as follows:

    $trna_object = TRNA->new(  );
  • An object. Here's an example:

    $trna_object->findloops(  );

    Perl sees that on the left side of the arrow $trna_object is an object of the TRNA class; it therefore calls the findloops method in the TRNA class. (It can see that $trna_object is a TRNA object because the object was blessed into the TRNA class when it was created by the new method, as I'll explain later.) Perl also passes a reference to the $trna_object object into the findloops method as the first argument to the method, shifting any other arguments that may have been explicitly given.

Why does Perl do it this way? The short answer to that question is that once you understand how it works, your code will become simpler and more usable. You will need to type class names less frequently, and you can use methods written for one class in another class (inheritance).

The two new tricks that Perl performs here are:

  • Using arrow notation to find the correct method in the correct class

  • Passing the method a new first argument

It can find the correct class because objects are blessed with their class name.