原文链接:https://www.javatpoint.com/perl-command-line-arguments
The simple command line options are done using ?s option. Complex command line options are done using Getopt::Std and Getopt::Long.
Getopt stands for GetOptions. It processes the content of @ARGV based on the configuration we give to it. It returns true or false value based on the processing.
Example:
In this example, we well get the age of the user from the command line.
use strict;
use warnings;
use 5.010;
use Getopt::Long qw(GetOptions);
my $x;
GetOptions('from=s' => \$x) or die "Usage: $0 --from NAME\n";
say"According to your age which is $x:";
if ($x >=18) {
say "You are eligible to vote";
}
We have declared a variable $x which will store value inserted into --from from the standard console. The from=s declares command line parameter called --from with a string after it. It is mapped further to the variable $x. The backslash () means we are passing a reference to the variable.
This script will switch to die part only when we will run this script by passing something that looks like a parameter name and starts with a (-) but is not declared in this script.
Now, we will run this program.
Output: