SoFunction
Updated on 2025-04-08

Use Getopt::Long module in Perl to receive user command line parameters

We often use a program that needs to add parameters in Linux. Now let’s learn about the module Getopt::Long in perl, which is much more powerful than using the array directly using @ARGV. I think everyone knows that some parameters in Linux have two forms.

•Long parameters –help
•Short parameters -h
That is, the - and - respectively.- represents the complete parameters.- represents the simplified parameters. These two methods are also supported in this module of Perl.
The two to be introduced in this: There are actually two modules, one is called Getopt::Long and the other is called Getopt::Std. Let's only introduce Getopt::Long. Because this module is more powerful

Getopt::Long module
Initialize the parameters accepted in the Perl command line, simplifying the analysis of command line parameters. Let’s see the program example below

Copy the codeThe code is as follows:

#!/usr/bin/perl
use strict;
use Getopt::Long;
use Smart::Comments;

my @libs    = (); 
my %flags   = (); 
my ( $verbose, $all, $more, $diam, $debug, $test, $step);

GetOptions(
        'verbose+'  => \$verbose,
        'more!'     => \$more,
        'debug:i'   => \$debug,
        'lib=s'     => \@libs,
        'flag=s'    => \%flags,
        'test|t'    => \$test,
        'all|everything|universe' => $all,
);

### $verbose
### $more
### $debug
### $test
### @libs;
### %flags

This is how to use it. Here is a detailed explanation. Pay attention to the previous part of => in GetOptions. Here is a detailed explanation.

•'verbose+'  The option with + does not receive variables, and there is no need to add content afterwards. Just use it directly. It will add variables every time it appears. That is, when -verbose -verbose appears twice in the parameter when -verbose -verbose appears twice, the value of verbose will become 2.
•'more!'                                                                                                                                                                                                                                                            �
•'flag=s'         A string connected with = requires a variable of type (s), an integer (i), or a floating point (f).
•'debug:i'       The option that is connected to : will accept optional variables with default 0 or an empty string
•'test|t'                                                            �
•'lib=s'    => @libs    If the associated variable is an array, such as @libs in this place, then the options can appear multiple times and the values ​​can be pushed into the array.
•'flag=s'   => %flags  If the associated variable is a hash, then a key=value pair is required and inserted into the hash.

Remark:
When matching parameter names, GetOptions will ignore case under default settings, and the default parameters are abbreviated as the unique shortest string (first letter) (for example, -m represents -more. When the same initial letter is used, a second letter will be added to distinguish it)

How to use the program of the Getopt module:

According to the above example, for example, we wrote a program called . We only need to add the following parameters to the command line:

Copy the codeThe code is as follows:
$ ./  --verbose --verbose -v --more \       --lib='/lib' -l '/lib64' --f a=1 --flag b=2  --debug 2 -t fukai

It's a little bit long, and you'll understand the meaning after looking at the above. In this place, I used the Smart::Comment module, so at the bottom ###  will output the content of this variable itself. This is also a super powerful module. Let's take a look at what content will be output after entering these parameters.

Copy the codeThe code is as follows:

### $verbose: 3
### $more: 1
### $debug: 2
### @libs: [
###          '/lib',
###          '/lib64'
###        ]
### %flags: {
###           a => '1',
###           b => '2'
###         }

I understand the parameters entered above.

A simple summary of the Getopt module

(1. Pass the value parameters into the program

※Argument type: integer, floating point number, string

Copy the codeThe code is as follows:

GetOptions(
    'tag=s' => \$tag
);

‘=’ means that this parameter must have parameter values. If you use ':' instead of indicating that the parameter does not necessarily have parameter values.
‘s’ means passing string parameters. If passing integer parameters to the 'i' table, and if passing floating point numbers to the 'f' table.

Methods for using values

Copy the codeThe code is as follows:

$ --tag=string
$ --tag string

(2. Multiple values ​​of parameters need to be transferred to the program.

For example, you need to pass several values ​​into @libfiles operation methods.

Copy the codeThe code is as follows:

GetOptions ("library=s" => \@libfiles);
GetOptions ("library=s@" => \$libfiles);

Parameters are passed to @$tag
How to use

Copy the codeThe code is as follows:
$ --library lib/stdlib --library lib/extlib

(3. Pass parameters to key-value pairs

Sometimes we need to transfer some key-value pairs to the program for processing, so we need to use this function.

Copy the codeThe code is as follows:

GetOptions ("define=s" => \%defines);
GetOptions ("define=s%" => \$defines);

How to use

Copy the codeThe code is as follows:
$ --define os=linux --define vendor=redhat

(4. Alias ​​for parameters
When we need to add an alias such as an abbreviation, we can use the following method

Copy the codeThe code is as follows:
GetOptions ('length|height=f' => \$length);

The first name is primary name, and the other names are alias (there can be multiple alias names). When using hash parameters, use primary name as the key value