Getopt::Long

If your program says:

use Getopt::Long;
GetOptions("verbose"  => \$verbose,
           "debug"    => \$debug,
           "output=s" => \$output);

it can be called from the command line like this:

% prog --verbose more args here
% prog --debug more args here
% prog -v -d more args here
% prog --output=somefile more args here
% prog -o somefile more args here

The Getopt::Long module provides a GetOptions function to process command-line switches with long names. It includes support for things like abbreviating switches, typed arguments like Boolean or string or integer or float, array variables for repeating options, user-defined validation routines, POSIX-conforming versus FSF-style processing, case-insensitive options, and traditional bundling of short options—just to name a few out of its vast cornucopia of features. If this module is overkill, consider the more traditional Getopt::Std module described next. If this module is underkill, check out the CPAN module Getopt::Declare, which provides a more declarative syntax for option specification.

Get Programming Perl, 3rd Edition 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.