4
votes

My company uses Getopt::Declare as its command line option parser. The structure of our option processing blocks routinely look like this:

Readonly my $ARGS => Getopt::Declare->new(
   join( "\n",
      "[strict]",
      "--engineacct <num:i>\tEngineaccount [required]",
      "--outfile <outfile:of>\tOutput file [required]",
      "--clicks <N:i>\tselect keywords with more than N clicks [required]",
      "--infile <infile:if>\tInput file [required]",
      "--pretend\tThis option not yet implemented. "
         . "If specified, the script will not execute.",
      "[ mutex: --clicks --infile ]",
   )
) || exit(1);

that's a lot to look at... I tried to make it a little simpler by using HEREDOCS like most of the documentation uses:

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all'; 

use Readonly; 

Readonly my $ARGS => Getopt::Declare->new(<<'EOPARAM');
  [strict]
  --client <client:i> client number [required] 
  --clicks <clicks:i> click threshold (must be > 5)
EOPARAM

While I think this is much easier to read, for some reason it won't recognize any of my arguments.

perl test.pl --client 5 --clicks 2

I get unrecognized arguments:

Error: unrecognizable argument ('--client')
Error: unrecognizable argument ('154')
Error: unrecognizable argument ('--clicks')
Error: unrecognizable argument ('2')

So I guess I have two quesitons:

  1. Has anyone successfully used HEREDOCS with Getopt::Declare?

  2. Is Getopt::Declare still a reasonable option for an option parser? As opposed to other modules like Getopt::Long

1
Getopt::Declare and Getopt::Long tend to be the most heavily used; I'd say either is a viable choice, depending on preference. - Vector Gorgoth

1 Answers

6
votes

In your original version, your string consists of --clicks <N:i> followed by a tab, followed by select keywords with more than N clicks [required].

In your revised version, your string has space instead of a tab.

Use <<"EOPARAM" and "\t" instead of <<'EOPARAM' and "".

>type x.pl
use Getopt::Declare;
Getopt::Declare->new(<<'EOPARAM');
  [strict]
  --client <client:i> client number [required]
  --clicks <clicks:i> click threshold (must be > 5)
EOPARAM

>perl x.pl --client 5 --clicks 2
Error: unrecognizable argument ('--client')
Error: unrecognizable argument ('5')
Error: unrecognizable argument ('--clicks')
Error: unrecognizable argument ('2')

(try 'x.pl -help' for more information)

>type x.pl
use Getopt::Declare;
Getopt::Declare->new(<<'EOPARAM');
  [strict]
  --client <client:i>\tclient number [required]
  --clicks <clicks:i>\tclick threshold (must be > 5)
EOPARAM

>perl x.pl --client 5 --clicks 2
Error: unrecognizable argument ('--client')
Error: unrecognizable argument ('5')
Error: unrecognizable argument ('--clicks')
Error: unrecognizable argument ('2')

(try 'x.pl -help' for more information)

>type x.pl
use Getopt::Declare;
Getopt::Declare->new(<<"EOPARAM");
  [strict]
  --client <client:i>\tclient number [required]
  --clicks <clicks:i>\tclick threshold (must be > 5)
EOPARAM

>perl x.pl --client 5 --clicks 2

>