2016-06-30 8 views
1

구문 분석 명령 줄 인수와 함께 내 perl 스크립트에 문제가 있습니다. 주로 필자는 hypel뿐만 아니라 (em/en) -dash와 함께 인수를 구문 분석하고 싶습니다. 다음 명령을 실행 고려하십시오 : 당신이 볼 수 있듯이Perl - en/em 대시 명령 줄 인수

my_spript.pl -firstParam someValue –secondParam someValue2 

, firstParam는 하이픈 접두어를하고 펄이 그것을 구문 분석에 아무 문제가 없다, 그러나 secondParam 펄이 그것을 인식 할 수없는 대시-KO 불행히도로 시작됩니다 인수로서. 당신이 Getopt::Long를 사용하는 경우 GetOptions에게주기 전에 사전 처리를 인수 할 수있는,

GetOptions(
    "firstParam" => \$firstParam, 
    "secondParam" => \$secondParam 
) 
+0

어떤 모듈을 사용하십니까? [Getopt :: Long] (http://p3rl.org/Getopt::Long)? – choroba

+0

@choroba, 예. Getopt :: Long을 사용합니다. – dejvid

+0

모든 점을 고려하여, 이것은 미친 요구 사항입니다. – tripleee

답변

3

: 나는 GetOptions을 사용하고 () 인수를 구문 분석

#! /usr/bin/perl 
use warnings; 
use strict; 

use Getopt::Long; 

s/^\xe2\x80\x93/-/ for @ARGV; 

GetOptions('firstParam:s' => \ my $first_param, 
      'secondParam:s' => \ my $second_param); 
print "$first_param, $second_param\n"; 

먼저 디코딩에 청소기 수 있습니다 비록 인수 :

use Encode; 

$_ = decode('UTF-8', $_), s/^\N{U+2013}/-/ for @ARGV; 

가, 다른 로케일 설정에서 작동 Encode::Locale을 사용하려면 :

#! /usr/bin/perl 
use warnings; 
use strict; 

use Encode::Locale; 
use Encode; 
use Getopt::Long; 

$_ = decode(locale => $_), s/^\N{U+2013}/-/ for @ARGV; 

GetOptions('firstParam:s' => \ my $first_param, 
      'secondParam:s' => \ my $second_param); 
print "$first_param, $second_param\n"; 
+0

OS-X에서는 문제가 없지만 Windows에서는 작동하지 않습니다. (perl 버전 : 5.14.2). 뭘 잘못 생각 했니? – dejvid

+1

Windows에서 터미널 인코딩은 UTF-8이 아닐 수 있습니다. 올바른 인코딩을 지정하십시오. – choroba

+0

@dejvid : 업데이트를 확인하십시오. – choroba