1
use PDF::Extract;
$pdf=new PDF::Extract( PDFDoc=>"test.pdf");
$i=1;
$i++ while ( $pdf->savePDFExtract( PDFPages=>$i ) );

I am trying to understand the above Perl code. It appears to be instantiating an object from a module. What is the argument in the line that calls the constructor? What does the => mean? Is it a hash argument?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
CJ7
  • 20,640
  • 59
  • 173
  • 305
  • Here is a pretty good explanation of what the fat comma can be used for. http://stackoverflow.com/questions/4093895/how-does-double-arrow-operator-work-in-perl – E Lewis Feb 02 '16 at 06:13

1 Answers1

5

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.

Matt Jacob
  • 6,268
  • 2
  • 22
  • 27