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.

When you are doing OO, mapping command-line options to your class attributes is convenient. But what if you are not using OO? There's Getopt::Attribute for that to map options to your package variables (there's also my Perinci::CmdLine that maps command-line options to function arguments, but I'm not reviewing my own modules in this series).

Getopt::Attribute is written by Marcel Grünauer (MARCEL), first in 2001 and last updated in 2010. Here are the users of this module on CPAN (mostly Marcel himself):

% lcpan rdeps Getopt::Attribute

+———+———-+——————–+——–+————–+————-+

| phase | rel | dist | author | dist_version | req_version |

+———+———-+——————–+——–+————–+————-+

| runtime | requires | Hopkins-Plugin-RPC | DIZ | 0.900 | 1.44 |

| runtime | requires | Module-Changes | MARCEL | 0.05 | 0 |

| runtime | requires | Module-Cloud | MARCEL | 1.100861 | 0 |

| runtime | requires | Task-MasteringPerl | BDFOY | 1.002 | 0 |

| runtime | requires | Vim-Complete | MARCEL | 1.100880 | 0 |

+———+———-+——————–+——–+————–+————-+



Here's how you would you Getopt::Attribute:

use Getopt::Attribute; our $verbose : Getopt(verbose!); our $all : Getopt(all); our $size : Getopt(size=s); our $more : Getopt(more+); our @library : Getopt(library=s); our %defines : Getopt(define=s); sub quiet : Getopt(quiet) { our $quiet_msg = 'seen quiet' } usage() if our $man : Getopt(man);

As you can see, it uses a rather Perl-specific feature called subroutine attributes. You can then call your CLI app like this:

% myapp –all –size=10 –more –more –library L1 –library L2



then your variable $all will be set to 1, $size to 10, $more to 2, and @library to ["L1", "L2"] .

The module code itself is surprisingly compact, less than 30 lines of code. If you wonder where the actual parsing is done, it's done in the INIT phase. So at least, unlike with App::Options, you can still utilize "perl -c" to syntax-check your scripts.

My main complaints are only: 1) my Emacs' cperl-mode still doesn't syntax-highlights these subroutine attributes correctly; 2) if you want to put all options to a single hash, you can't, so this module forces you to pick a particular style.

This module is a pure Getopt::Long wrapper that does not add additional features like putting summary string for each option (although that's doable putting it in the subroutine attribute as parameter), specifying required option, or specifying default value. It would make the module more interesting if it had those features.