これも昔メモったシリーズだけど。
Getopt::Longの場合
use Getopt::Long; my %args; my $result = GetOptions ("integer=i" => \$args{'integer'}, "string=s" => \$args{'string'}, "flag" => \$args{'flag'} ); # 各指定されたパラメータで処理を行う。 print "integer=" . $args{'integer'},"\n", "string=" . $args{'string'} , "\n", "flag=" . $args{'flag'} ,"\n";
実行するとこんな具合。
$ perl args.perl --integer 1 --flag --string hello integer=1 string=hello flag=1
Getopt::Stdの場合
こちらはちょっと特殊。$opt_?という変数に値を入れちゃう。
getopt関数
use Getopt::Std; getopt("abc"); # 各指定されたパラメータで処理を行う。 print $opt_a,"\n", $opt_b,"\n", $opt_c,"\n";
実行はこんな具合
$ perl args.perl -a aaa -b bbb -c ccc aaa bbb ccc
オプションを指定しないと無視される。
$ perl args.perl -a aaa -b bbb aaa bbb
getopts関数
こっちは後ろにコロンをつけたものだけ値をとる。
あとは指定されれば1、そうでなければ空。
use Getopt::Std; getopts("a:bc"); print $opt_a,"\n", $opt_b,"\n", $opt_c,"\n";
$ perl getargs.perl -a aaa -b -c aaa 1 1 $ perl getargs.perl -a aaa -b aaa 1