In linux, we often use a program that requires adding parameters. Now let's take a look at the Getopt: Long module in perl, which is much more powerful than the array directly using @ ARGV. I want to know that there are two types of parameters in Linux.
• Long parameter-help
• Short parameter-h
That is,-and-respectively.-indicates the complete parameter.-indicates the simplified parameter. These two methods are also supported in this module of Perl.
This article introduces two Getopt modules. One is Getopt: Long and the other is Getopt: Std. Here we will only introduce Getopt: Long. This module is more powerful.
Getopt: Long Module
Initialize the parameters accepted in the Perl Command Line to simplify the parsing of command line parameters.
Copy 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 the method used. The following is a detailed explanation. Note that the => above section in GetOptions is described below.
• The 'verbose + 'option does not receive variables and does not need to be added later. you just need to use it directly. A variable will be added each time it appears, that is, the value of verbose will be changed to 2 when the "verbose-verbose" parameter appears twice in the command line.
• 'More! 'Connected! The option does not receive the variable (that is, you do not need to add the parameter-more to use it later), as long as this parameter appears in the command line, the default value is 1, is used to set to enable and disable a function>. you can add "no" before the parameter to change to "negative", for example, "-nomore.
• The 'flag = S' string must be string (s), INTEGER (I), or floating point (f.
• The 'debug: I 'option with: will accept optional variables with the default value 0 or empty string
• The 'test | T' option can be abbreviated to-t for-test.
• 'Lib = s' => @ libs if the associated variable is an array, such as @ libs in this region, the option can appear multiple times and the value can be pushed to the array.
• 'Flag = s' => % flags if the associated variable is a hashed column, a key = value (key = value) pair is required, and is inserted into the hash.
Note:
When the parameter name is matched, GetOptions ignores case sensitivity by default. The default parameter is abbreviated as a unique shortest string (first letter) (for example,-m indicates-more. when the first letter is the same, the second letter will be added to distinguish)
The procedure of the Getopt module is as follows:
Based on the example above, for example, we wrote a program named test. pl. We only need to add the following parameters in the command line:
Copy codeThe Code is as follows: $. /test. pl -- verbose-v -- more \ -- lib = '/lib'-l'/lib64' -- f a = 1 -- flag B = 2 -- debug 2-t fukai
It's a little long. When you look at the above, you will understand the meaning. in this place, I use the Smart: Comment module, so the following ### will output the content of this variable. this is also a super powerful module. let's take a look at the input parameters. what will be output.
Copy codeThe Code is as follows: ### $ verbose: 3
### $ More: 1
### $ Debug: 2
###@ Libs :[
### '/Lib ',
### '/Lib64'
###]
### % Flags :{
### A => '1 ',
### B => '2'
###}
Check the parameters entered above.
Summary of the Getopt Module
(1. Pass in the program with a value Parameter
※Parameter type: integer, floating point, and string
Copy codeThe Code is as follows: GetOptions (
'Tag = s' = >\$ tag
);
'=' Indicates that this parameter must have a parameter value. If you use ':' instead, it indicates that the parameter does not have to have a parameter value.
'S' indicates passing string parameters. If it is an 'I' table, passing Integer Parameters, if it is an 'F' table, passing floating point numbers.
Method for using a parameter with a value
Copy codeThe Code is as follows: $ test. pl -- tag = string
$ Test. pl -- tag string
(2. Parameters of multiple values need to be transferred to the program.
For example, you need to upload several values to @ libfiles.
Copy codeThe Code is as follows: GetOptions ("library = s" = >\@ libfiles );
GetOptions ("library = s @" = >\$ libfiles );
Parameter transfer @ $ tag
Usage
Copy codeThe Code is as follows: $ test. pl -- library lib/stdlib -- library lib/extlib
(3. Pass the key-Value Pair Parameter
Sometimes we need to send some key-value pairs to the program for processing, so we need to use this function.
Copy codeThe Code is as follows: GetOptions ("define = s" => \ % defines );
GetOptions ("define = s %" = >\$ defines );
Usage
Copy codeThe Code is as follows: $ test. pl -- define OS = linux -- define vendor = redhat
(4. Parameter alias
The following method can be used when you need to add an alias such as abbreviation to a parameter:
Copy codeThe Code is as follows: GetOptions ('length | height = f' = >\$ length );
The first name is primary name and the other names are alias (multiple alias names are allowed). When the hash parameter is used, the primary name is used as the key value.