About this mini-article series. Each day for 24 days, I will be reviewing a module that parses command-line options (such module is usually under the Getopt::* namespace). First article is here.

Getopt::Compact is a module that was first released in 2004 by Andrew Stewart Williams (ASW) and last updated in 2006. It manages to have 7 CPAN distributions depending on it.

Like Getopt::Long::Descriptive, this module is a wrapper for Getopt::Long mainly to allow users to specify summary string for each option as that's what is lacking in Getopt::Long to produce a useful usage/help message. Getopt::Compact also tries to present a different interface that claims to be more compact if you have a lot of (flag) options. For example, this code using Getopt::Long:

GetOptions( "--flag1" => \$opts{flag1}, "--flag2" => \$opts{flag1}, "--flag3" => \$opts{flag1}, "--flag4" => \$opts{flag1}, "--val1|1=s" => \$opts{val1}, "--val2|2=i" => \$opts{val2}, "--val3=s@" => $opts{val3}, );

will become like this when using Getopt::Compact:

my $opts = Getopt::Compact->new( modes => [qw/flag1 flag2 flag3 flag4/], struct => [ [["val1", "1"], "Value1 blah blah"], [["val2", "2"], "Value2 blah blah", "=i"], ["val3" , "Value3 blah blah", "=s@"], ], )->opts;

But if you always put option values into hash elements (instead of sometimes assigning an option handler), GetOptions provides an alternative interface in which you specify hashref as first argument. This makes for a more compact syntax:

GetOptions( \%opts, [qw/--flag1 --flag2 --flag3 --flag4 --val1|1=s --val2|2=i --val3=s@/], );

So basically what Getopt::Compact makes you do is specifying option in split parts: –name|a=s@ in Getopt::Long becomes:

[["name","a"], "summary", "=s@"]



I recommend using Getopt::Long::Descriptive (GLD) instead of this because: 1) the interface is slightly nicer (no split option specification so more familiar to Getopt::Long users); 2) GLD allows specifying default value for options; 3) GLD allows expressing that an option is required.