[Perl] getopt function to receive USER 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.

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:

12345678910
#!/usr/bin/perl -wuse strict;use Getopt::Std; use vars qw($opt_a $opt_b $opt_c);getopts('a:b:c:'); 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;

The output is as follows:

[Root @ mail test] #./getopt. pl-a aa-B BB-C CC

$ Opt_a =>; AA

$ Opt_ B =>; bb $ opt_c =>; CC

 

Complete Parameters

12345678910111213141516171819202122232425
 #!/usr/bin/perl  use Getopt::Long;  Getopt::Long::GetOptions(                    'page=i'    => \$page,                   'onoff!'    => \$onoff,                     'help'      => \$wants_help,                  'name=s'    => \$name,                  'number:i' => \$number);  } if(defined($page)){   print "page flag set to $page " }if(defined($onoff)){           print "onoff flag set to $onoff ";}if(defined($wants_help)){           print "help flag set to $wants_help ";}if(defined($name)){           print "name flag set to $name ";}if(defined($number)){           print "number flag set to $number ";}

./Getlong. pl-name aaa
Name flag set to AAA

 

Parameter with value
※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
Example:
Test. pl-tag = string
Or
Test. pl-tag string

 

Parameters with multiple parameter values

Getoptions ("library = s" = >\@ libfiles );
Parameter transfer @ tag
Or
Getoptions ("library = s @" = >\$ libfiles );
Parameter transfer @ $ tag
Example:
Test. pl-library lib/stdlib-library lib/extlib

 

Parameter alias
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, primary name is used as the key value.

Abbreviation and Case sensitivity of parameters
Getoptions ('length | Height = f' = >\$ length, "head" = >\$ head );
If not specified,GetoptThe case sensitivity of the parameter is ignored, that is, the values of-l or-l indicate
Is the same parameter (-length)

For options that do not pass parameters, that is, some switch types, you can connect "!" after the first part, This indicates that this option does not receive the independent variable, but it can be changed to negative by adding no before (for example,-nomore of the "more" option ). If "!" is not used, It is "+", which indicates that it will add a variable every time it appears. If the option appears in the command line, the related variable is set to 1; if the negative option appears, the related variable is set to 0.

 

Hash parameter value (with name and parameter value)
Getoptions ("define = s" => \ % defines );
Or
Getoptions ("define = S %" = >\$ defines );
Example:
Test. pl-define OS = Linux-define vendor = RedHat

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.