5.5. Taking the Long Way Around

There's an alternative way of option parsing using the core module Getopt::Long:

use Getopt::Long;
use constant WEB => 1;
use constant SQL => 2;
use constant REG => 4;

my %dbg_flag = (
   WEB => WEB,
   SQL => SQL,
   REG => REG);
my %dbg;

GetOptions(\%dbg, "D=s@");
my $DEBUG = WEB | REG unless $dbg{D};

$DEBUG |= $dbg_flag{$_}
       || die "Unknown debug flag $_\n"
   foreach @{$dbg{D}};

(We've shown only the option-parsing part.) This has a slightly different interface; instead of separating multiple values with commas, we must repeat the -D flag:

% whizzbang.pl -D WEB -D REG
				

(We could also say “-D=WEB -D=REG”.)

Getopt::Long permits many more choices than this, of course. If you have a complex program (particularly one worked ...

Get Perl Debugged now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.