We often use a program in Linux to add parameters. Now let's take a look at the functions related to control parameters in Perl. getopt. in Linux, there are two types of parameters. one is -- help, and the other is-H. that is, the difference between-and. -- Indicates the complete parameter.-indicates the simplified parameter.
These two types are also available in Perl.
Getopt: STD module function: initializes parameters accepted in the Perl Command Line and simplifies the parsing of command line parameters. Example of simplified parameters:
#! /Usr/bin/perl-W
Use strict;
Use getopt: STD;
Use vars QW ($ opt_a $ opt_ B $ opt_c );
Getopts ('d: F: P :');
Print "\ $ opt_a = >;$ opt_a \ n" If $ opt_a;
Print "\ $ opt_ B = >;$ opt_ B \ n" If $ opt_ B;
Print "\ $ opt_c = >;$ opt_c \ n" If $ opt_c;
Output: [[email protected] Test] #. /getopt. pl-a aa-B bb-c cc $ opt_a =>; AA $ opt_ B =>; bb $ opt_c =>; CC
[Describe "D: F: P", D and F are followed by colons, indicating-D, and-F is followed by parameters. There is no colon after P, indicating that-P is not followed by a parameter.
And the parameters after-D and-F are assigned to the variables $ opt_d and $ opt_f respectively. For $ opt_p, if-P is added to the command line, $ opt_p = 1; otherwise, it is 0].
Complete Parameters
Getopt: Long, more powerful than the array directly using @ argv.
Initializes parameters accepted in the Perl Command Line to simplify the parsing of command line parameters. The following is an example of a program.
#! /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,
);
Below is a detailed explanation
- The 'verbose + 'option is followed by the + option 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 is connected to |, indicating that-test can be abbreviated to-t.
- '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 hash, a key = value (Key = value) pair is required and 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:
$./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 the variable itself. This is also a super powerful module. Let's take a look at the input parameters. What will be output.
### $ 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
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
$ 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.
GetOptions ("library=s" => \@libfiles);GetOptions ("[email protected]" => \$libfiles);
Parameter transfer @ $ tag
Usage
$ 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.
GetOptions ("define=s" => \%defines);
GetOptions ("define=s%" => \$defines);
Usage
$ 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:
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.