Perl Module Basics
uses the arrow operator, >. The left hand side of the arrow operator must be either
a package name (such as IO::File) or an object (such as $filehandle). The right
hand side is the name of a subroutine to call.
Methods automatically receives as an extra initial parameter the variable on
the left hand side of the arrow operator. You can imagine that the call to new() is
translated into the following:
my $filehandle = IO::File::new("IO::File", "> file.txt");
But you shouldn't write it that way using method call syntax enables Perl's inher
itance to work. I'll describe inheritance in more detail later.
Perl offers another method call syntax known as indirect object syntax. Here's
the code from the example rewritten to use indirect object method calls:
my $filehandle = new IO::File ">file.txt";
print $filehandle "This line goes into file.txt\n";
close $filehandle;
In this style, the method name comes first followed by either a package name or an
object. Both calling styles result in the same method invocation the extra initial
argument is supplied to the method subroutine in both cases. Choosing which one
to use is largely a matter of preference, although many Perl programmers prefer
the arrow notation since it is less visually ambiguous. Furthermore, Perl itself
occasionally has trouble parsing indirect object syntax. For these reasons, I'll be
using arrow notation in my examples from this point forward.
CAUTION C++ programmers take note there is nothing special about
methods named new(). It is only by convention that constructors are
often named new().
A method that is called using a package name is a class method. A method
called with an object is an object method. Class methods are used to provide services
that are not specific to any one object; object construction is the most common
example but I'll explore others in the next sections.
39
39
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved