How to use Getopt::Long with boolean flags in Perl?
Question
How to use Getopt::Long with boolean flags in Perl?
Using Getopt::Long with Boolean Flags in Perl
Getopt::Long is a powerful core Perl module designed to handle command-line options with flexible syntax. When dealing with boolean flags—options that act as simple switches turning on or off a behavior—Getopt::Long provides an intuitive way to capture these flags as Perl scalar variables with values 1 (true) or 0 (false).
Basic Usage
Boolean flags in Getopt::Long are declared by assigning an option specification with = and an exclamation mark (!). This syntax means the option accepts an optional value; if the value is omitted, the flag is treated as a boolean toggle.
--flagsets the corresponding variable to true (1).--noflagsets it false (0).- You can also explicitly pass
=1or=0to enable or disable.
Using the exclamation mark suffix is the recommended way to handle boolean options since it recognizes both positive and negative forms, making scripts more user-friendly.
Example Program
use strict;
use warnings;
use Getopt::Long;
# Declare variables for boolean flags
my $verbose = 0;
my $debug = 0;
# Configure Getopt::Long to process command-line options
GetOptions(
'verbose!' => \$verbose,
'debug!' => \$debug,
) or die "Error in command line arguments\n";
print "Verbose mode is ", ($verbose ? "ON\n" : "OFF\n");
print "Debug mode is ", ($debug ? "ON\n" : "OFF\n");
Save this as flags.pl and run it from the command line, for example:
perl flags.pl --verbosesets verbose to true.perl flags.pl --noverbosesets verbose to false explicitly.perl flags.pl --debug=1or--debug=0sets debug true or false.- Omitting options leaves them at the initialized default value (
0in this case).
Important Concepts and Gotchas
- Sigils and References: The option values must be passed as scalar references (i.e.,
\$verbose), so Getopt::Long can update those variables. - Exclamation Mark '!': This tells Getopt::Long to accept both positive and negative forms of the option.
- Case Sensitivity: Option names are case sensitive unless you enable
Getopt::Long::Configure("ignore_case"). - Defaults matter: Always initialize boolean variables before calling
GetOptions, as unspecified flags leave the variables unchanged. - Deprecated syntax: Older style scalars like
:swon’t correctly handle boolean toggles.
Version Considerations
Getopt::Long has been stable for a long time; the exclamation mark boolean flag notation and standard behavior have existed since at least Perl 5.6+. If you use Perl 5.10+ or later (which is common today), you can rely on this without concern. Always check your Perl version with perl -v if unsure.
Summary
To use boolean flags with Getopt::Long:
- Declare variables with an initial value (usually 0).
- In
GetOptions, use option names with an appended!(e.g.,'flag!'). - Pass the variables as references.
- Use the flags on the command line with
--flagto enable or--noflagto disable.
This approach makes your command-line scripts robust and user-friendly while embracing Perl’s flexible philosophy of “There’s More Than One Way To Do It” (TMTOWTDI).
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 30ms
Verbose mode is OFF
Debug mode is OFF
(empty)Was this helpful?
Related Questions
- How to use File::Slurp to read entire file into variable in Perl?
- How to get file modification time with Path::Tiny in Perl?
- How to use Path::Tiny touch to create empty file in Perl?
- How to use List::Util reduce for custom aggregation in Perl?
- How to compare two DateTime objects in Perl?
- How to use DateTime::Duration for time periods in Perl?