2014-02-25 7 views
1

링크 Parse::ABNF perl usage에 게시 된 질문에 대한 문의 주셔서 감사합니다. 나는 아직도 내 문제를 해결하는 데 어려움에 직면 해있다. 아래에서 내 문제를 확인하고 솔루션에 대한 조언을 제공 할 것을 요청하십시오.perl을 사용하여 구문 분석 이상 문법

지금은 ABNF 형식의 한 마디 문법을 파일 (sip.abnf)로 작성했습니다. 나는 (recd_message.txt) 아래와 같이 파일에 헤더와 SIP 메시지가 :

:
From: <sip:[email protected]:5060;user=phone>;tag=1526438727-1338998848384- 
To: "govoice-lab2-aokanlawon"<sip:[email protected]> 
Contact: <sip:[email protected]:5070;transport=udp> 
Allow: ACK,BYE,CANCEL,INFO,INVITE,OPTIONS,PRACK,REFER,NOTIFY 
Accept: multipart/mixed,application/media_control+xml,application/sdp 

내가 컨텐츠 아래로 testSipHeader.pl로 이름 위의 헤더 메시지의 유효성을 검사 ABNF 문법을 사용하는 펄 프로그램을 만들어
use strict; use warnings; 
use File::Slurp; 
use Parse::ABNF ; 
use Data::Dumper; 

my $grammar_file = shift; 
my $messages = shift; 
my @header_name; 
my $header_status; 

my $grammar = scalar read_file($grammar_file); 
$grammar =~ s/\x0d\x0a/\n/g; 
$grammar =~ s/^\s+(?=[\w-]+\s*=)//mg; 

$grammar = Parse::ABNF->new->parse( scalar read_file($grammar_file)); 
if (defined $grammar) { 
    print "Grammar is now defined...\n"; 
    my $header; 
    open ($header , "<", $messages) or die "Could not open $messages file"; 
    while(<$header>) { 
     print "Processing the message $_\n"; 
     @header_name = split(': ', $_); 
     if ($header_name[0] eq "From") { 
      $header_status = $grammar->From($_) ; 
     } elsif ($header_name[0] eq "To") { 
      $header_status = $grammar->To($_) ; 
     } elsif ($header_name[0] eq "Contact") { 
      $header_status = $grammar->Contact($_) ; 
     } elsif ($header_name[0] eq "Allow") { 
      $header_status = $grammar->Allow($_) ; 
     } elsif ($header_name[0] eq "Accept") { 
      $header_status = $grammar->Accept($_) ; 
     } else { 
      print "Error: Unsupported header $header_name[0] received\n"; 
     } 
    } 
} else { 
     print "Error: grammar is not defined\n"; 
} 

펄 invokation 및 출력/오류가

$> perl -I. testSipHeader.pl sip.abnf recd_messages.txt 
Grammar is now defined... 
Processing the message From: <sip:[email protected]:5060;user=phone>;tag=1526438727- 

Can't call method "From" on unblessed reference at testSipHeader.pl line 21, <$header> line 1. 

주 이하 : 현재 내가 GENER C 프로그램이 ates the SIP headers, 그리고이 perl 함수로 헤더의 내용을 검증하려고합니다. 링크에 주어진 그리고 Parse::RecDescent grammar not working as expected

내가 구문 분석 :: ABNF을 포함하고 입력 파일을 처리하기 위해 스크립트에서 약간 수정했고, 그 이후에 접수 된 출력 경로에 존재하는 사용과 비슷한 사용하는 것을 시도하고있다 https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing

+0

왜 여러 곳에서 '스칼라'를 사용합니까? – user1126070

답변

0

제가 약간의 스크립트를 재 작성했습니다. 출력은 무엇입니까?

use Data::Dumper; 
my $grammar_rules; 
{ 
    local $/=undef; 
    open(my $fh,'<',$grammar_file) or die $grammar_file,$!; 
    $grammar_rules = <$fh>; 
    $grammar_rules =~ s/\x0d\x0a/\n/g; 
    $grammar_rules =~ s/^\s+(?=[\w-]+\s*=)//mg; 
} 
print Dumper('rules',$grammar_rules); 
my $grammar = Parse::ABNF->new->parse( $grammar_rules); 
print Dumper('grammar',$grammar); 
die "Error: grammar is not defined" if ! defined $grammer; 
print "Grammar is now defined...\n"; 

open (my $header_fh , "<", $messages) or die "Could not open $messages file, $!"; 
while(my $line = <$header_fh>) { 
    chomp($line); 
    print "Processing the message $line\n"; 
    @header_name = split(': ', $line); 
    if ($header_name[0] eq "From") { 
     $header_status = $grammar->From($line) ; 
    } elsif ($header_name[0] eq "To") { 
     $header_status = $grammar->To($line) ; 
    } elsif ($header_name[0] eq "Contact") { 
     $header_status = $grammar->Contact($line) ; 
    } elsif ($header_name[0] eq "Allow") { 
     $header_status = $grammar->Allow($line) ; 
    } elsif ($header_name[0] eq "Accept") { 
     $header_status = $grammar->Accept($line) ; 
    } else { 
     print "Error: Unsupported header $header_name[0] received\n"; 
    } 
}