C 언어를 이용할 경우 getopt(3)함수를 통해서 실행라인 인자를 철할 수 있다. perl(:12)의 경우 getopts를 이용해서 인자를 처리할 수 있다. 사용방법도 C(:12)의 그것과 비슷하다.
use Getopt::Std;
getopt('oDI', \%opts); # -o, -D, -I 인자를 처리해서 해쉬변수인 opts에 저장한다.
getopt('oif:'); # -o, -i 는 boolean flag 이다. -f 는 값을 필요로 한다.
#!/usr/bin/perl
use Getopt::Std;
sub myfunc
{
$file = shift;
print "file name : $file\n";
}
sub version
{
print "Version 1.1\n";
}
sub help
{
print "Usage : ./getopt -h -v -f [file name]\n";
}
###########
# MAIN
###########
### GETOPT ###############
%opts = ();
getopts("hvf:", \%opts);
my $command;
# GET MODULE LIST & VERSION
if (defined $opts{h})
{
help();
exit 0;
}
if (defined $opts{f})
{
myfunc($opts{f});
}
if (defined $opts{v})
{
version();
}
http://perldoc.perl.org/Getopt/Std.html
GetOptions
GetOptions를 이용하면 좀더 자유롭게 실행인자를 다룰 수 있다.
#!/usr/bin/perl
use Getopt::Long;
GetOptions("h" => \&help,
"v" => \&version,
"debug=s" => \$mystring,
"float=f" => \$myfloat,
"int=i" => \$myint);
print "mystring $mystring\n" if $mystring;
print "myfloat $myfloat\n" if $myfloat;
print "myint $myint\n" if $myint;
sub help
{
print "Help function\n";
exit;
}
sub version
{
print "Version 1.0\n";
exit;
}
getopt
GetOptions
Recent Posts
Archive Posts
Tags