The constructor is called via indirect object syntax, which is discouraged (and usually a sign of old code). It would be better written as:
my $pdf = PDF::Extract->new(...);
The perlobj documentation recommends you avoid indirect object syntax for the following reasons:
First, it can be confusing to read. In the above example, it's not
clear if save
is a method provided by the File
class or simply a
subroutine that expects a file object as its first argument.
When used with class methods, the problem is even worse. Because Perl
allows subroutine names to be written as barewords, Perl has to guess
whether the bareword after the method is a class name or subroutine
name. In other words, Perl can resolve the syntax as either
File->new( $path, $data )
or new( File( $path, $data ) )
.
To answer your second question, the =>
is known as the fat comma, and perlop has this to say about it:
The =>
operator (sometimes pronounced "fat comma") is a synonym for
the comma except that it causes a word on its left to be interpreted
as a string if it begins with a letter or underscore and is composed
only of letters, digits and underscores. This includes operands that
might otherwise be interpreted as operators, constants, single number
v-strings or function calls. If in doubt about this behavior, the left
operand can be quoted explicitly.
Otherwise, the =>
operator behaves exactly as the comma operator or
list argument separator, according to context.
In your example code, the constructor receives a list, just like if you had used a normal comma. In fact, your code is equivalent to this:
my $pdf = PDF::Extract->new('PDFDoc', 'test.pdf');
However, the thing that creates the hash is the assignment on the other side, which may look something like this:
sub new {
my $class = shift;
my %args = @_;
# ...
}
The fat comma isn't used exclusively with hashes (nor is it required to initialize a hash, as I pointed out above), but you will typically see it anywhere there's a key/value association in a list of arguments. The shape of the characters makes it clear that "this is related to that". It also saves some typing of quote characters, which is a nice side benefit.