Use the getopt: Long module in Perl to receive USER command line parameters.

Source: Internet
Author: User
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. -Complete parameters. -Simplified Parameters

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 is the difference between-and. -Complete parameters. -Simplified parameters. these two methods are also supported in the Perl module. this article introduces two getopt modules. One is getopt: long and the other is getopt: STD. the following describes only getopt: Long.

This module is more powerful.Getopt: Long ModuleInitialize the parameters accepted in the Perl Command Line to simplify the parsing of command line parameters.

The 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:

The 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.

The 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

The Code is as follows:

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

'=' Indicates that this parameter must have a parameter value. If ':' is used instead of 's', it means that the parameter does not have to have a parameter value. If 'I' is used, an integer parameter is passed, if it is a 'F' table, the floating point number is transferred.

Method for using a parameter with a value

The 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.

The Code is as follows: getoptions ("library = s" = >\@ libfiles );
Getoptions ("[email protected]" = >\$ libfiles );

How to transmit parameters to @ $ tag

The 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.

The Code is as follows: getoptions ("define = s" => \ % defines );
Getoptions ("define = S %" = >\$ defines );

Usage

The Code is as follows:

$ Test. pl -- Define OS = Linux -- Define vendor = RedHat

(4. Parameter aliasThe following method can be used when you need to add an alias such as abbreviation to a parameter:

The 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.