#!/usr/bin/perl
use Getopt::Long;
$Getopt::Long::ignorecase = 0;
my %opt; #声明一个哈希,用于存放从命令行提取到的option
GetOptions
(
\%opt,
'--null',
'--string=s',
'--integer=i',
'--float=f'
);
#执行命令时,可以带下面这几个选项
#-null ,没有参数
#-string ,带参数,参数为字符串类型
#-integer ,带参数,参数为整数类型
#-float ,带参数,参数为浮点类型
if( $opt{'null'})
{
print"opt null exist."."\n";
}
if( $opt{'string'})
{
print"opt string exist:".$opt{'string'}."\n";
}
if( $opt{'integer'})
{
print"opt integer exist:".$opt{'integer'}."\n";
}
if( $opt{'float'})
{
print"opt float exist:".$opt{'float'}."\n";
}

执行后:

Perl脚本解析命令行参数
这段Perl代码演示了如何使用Getopt::Long模块来处理命令行参数。它定义了四个可选参数:--null(无参数)、--string(字符串参数)、--integer(整数参数)和--float(浮点数参数),并分别检查这些参数是否存在,如果存在则打印相应的信息。
1036





