use Getopt::Std;
getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect.
getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
# Sets $opt_* as a side effect.
getopts('oif:', \%opts); # options as above. Values in %opts
在Perl中,可以用getopts()调用来处理命行参数。
1 #!/usr/bin/perl -w
2
3 use Getopt::Std;
4
5 my %opts;
6
7 getopts('v:h',\%opts);
8
9 if ($opts{'v'}) {
10 printf("%s\n", $opts{'v'}); #./xxx -v xxx
11 }
12
13 if ($opts{'h'}) {
14 printf("%s\n", $opts{'h'}); # 1
15 }
or