2012-07-21 36 views
2

我从VLM telnet服务的一些数据:标签转换成文本Perl数据格式

show 
    media : (1 broadcast - 0 vod) 
     cam1 
      type : broadcast 
      enabled : yes 
      loop : no 
      inputs 
       1 : rtsp://xxx:[email protected]:xxx/xxxx/xxx.xxx 
      output : #transcode{vcodec="h264"}:standard{access=http,mux=ts,dst=xxx.xxx.xxx.xxx:6690/cam1} 
      options 
      instances 
       instance 
        name : default 
        state : playing 
        position : 0,000000 
        time : 0 
        length : -1 
        rate : 1,000000 
        title : 0 
        chapter : 0 
        can-seek : 0 
        playlistindex : 1 
    schedule 

在这里该数据转换成XML或JSON或其他的Perl的方式支持的格式(哈希表等)?

回答

2

这个数据是非常接近YAML,也许是故意如此。所有你需要做的是

  • 添加一个初始行---标记内容

  • 删除像(1 broadcast - 0 vod)所有评论的开始。

  • 添加尾随冒号目前不包含一个

所有行现有的评论将被罚款只是media节点不能同时等于评论和容器cam1节点。

该程序编辑数据以形成适当的YAML,将其加载到Perl哈希中并转储结果。

use strict; 
use warnings; 

use YAML 'Load'; 

open my $fh, '<', 'VLM.txt' or die $!; 

my $yaml = "---\n"; 

while (<$fh>) { 
    s/\s*\(.*//; 
    s/$/ :/ unless /:/; 
    $yaml .= $_; 
} 

my $data = Load($yaml); 

use Data::Dump; 
dd $data; 

输出

{ 
    show => { 
    media => { 
     cam1 => { 
     enabled => "yes", 
     inputs => { 1 => "rtsp://xxx:xxx\@xxx.xxx.xxx.xxx:xxx/xxxx/xxx.xxx" }, 
     instances => { 
         instance => { 
         "can-seek"  => 0, 
         "chapter"  => 0, 
         "length"  => -1, 
         "name"   => "default", 
         "playlistindex" => 1, 
         "position"  => "0,000000", 
         "rate"   => "1,000000", 
         "state"   => "playing", 
         "time"   => 0, 
         "title"   => 0, 
         }, 
        }, 
     loop  => "no", 
     options => undef, 
     output => "#transcode{vcodec=\"h264\"}:standard{access=http,mux=ts,dst=xxx.xxx.xxx.xxx:6690/cam1}", 
     type  => "broadcast", 
     }, 
    }, 
    schedule => undef, 
    }, 
} 
+0

哇!很棒。谢谢。 – arkhamvm 2012-07-22 23:01:25