[Perl] using the Geopt::long module to receive command-line arguments

Source: Internet
Author: User

There are two ways in which a Perl program needs to handle user input parameters:

The first method is to use the @argv array

The second approach is to call the Getopt::long module, which is convenient and powerful. This article mainly introduces this method.

Getopt::long invocation instance in Perl:

#!/usr/bin/perluse getopt::long;my ($verbose, $monitor, $debug, $test);
My @libs = ();
My%flags = ();


' monitor! '
' Debug:i '
' Test|t=s '
' Lib=s '
' Flag=s '
);

Print "verbose= $verbose, monitor= $monitor, debug= $debug, test= $test, [email protected]\n];
while (My ($k, $v) =each%flags) {
print "$k = $v \ n";
}

Code Explanation:

1. ' verbose+ ': ' + ' means that you do not need the option followed by an argument, and each time you use this option verbose the variable automatically plus 1.

For example:-verbose-verbose appears in the command line 2 times, verbose value becomes 2.

2. ' monitor! ': "!" Indicates that the option does not need to be followed by parameters and can be used directly (-monitor). At this point the default value for more is 1.

Often used to set open and close a feature,-monitor means open,-nomonitor indicates off.

3. ' Debug:i ': ': ' indicates that the argument followed by this option can be 0 or a null character

4. ' test|t=s ': ' | ' means that the option can be abbreviated, such as-test can be shortened to-t

"=" indicates that the option must be followed by a parameter

5. ' lib=s ' + \ @libs: If libs is an array variable, this option can occur more than once, and the period value is stored in arrays.

6. ' flag=s ': ' = ' indicates that the option is followed by a parameter of type string (s), Integer (i), or floating-point number (f).

7. ' Flag=s ' + \%flags: If Flags is hashed, the parameter of this option requires a key-value pair (Key=value) whose value is stored in the hash.

Note: When parameters match, the case is ignored, and its options can be abbreviated (the only shortest string, such as the first letter:-M for-more)

Getopt Program Execution Method:

Assuming the above code is saved in the test.pl file, chmod +x test.pl.

$./test.pl--verbose--verbose-v--monitor--lib= '/lib '-l '/lib64 '--f a=1--flag b=2  --debug 2-t Random

Executing the above code, you can get the following result:

Verbose=3, Monitor=1, debug=2, Test=random, libs=/lib/lib64a=>1b=>2

Getopt::long Module Pass Parameters:

1. Parameter type: S is a string, I is an integer, and F is a floating-point number

2. Variable type: $ is a normal variable, @ is an array, and% is a hash

[Perl] using the Geopt::long module to receive command-line arguments

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.