Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Perl에서 getopt 사용

C 언어를 이용할 경우 getopt(3)함수를 통해서 실행라인 인자를 철할 수 있다. 경우 getopts를 이용해서 인자를 처리할 수 있다. 사용방법도 [wiki:Site/C C의 그것과 비슷하다.

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

히스토리

  • 수정
    • : 링크 정리