2010-04-26 53 views
4

这里是我想在Perl脚本做:如何从Perl中的XML文件中提取和保存值?

 
$data=""; 
sub loadXMLConfig() 
{ 
    $filename="somexml.xml" 
    $data = $xml->XMLin($filename); 
} 

sub GetVariable() 
{ 
    ($FriendlyName) = @_; 
    switch($FriendlyName) 
    { 
     case "My Friendly Name" {print $data->{my_xml_tag_name}} 
     .... 
     .... 
     .... 
     } 
} 

是我使用的Perl只是因为我从一个XML文件中读取,但我需要一个shell脚本来获得这些变量的问题。所以,这里是我使用的是什么:

$ perl -e 'require "scrpt.pl"; loadConfigFile(); GetVariable("My Variable")' 

这个工程完全按照预期,但我需要阅读每一次我得到一个变量中的XML文件。有没有一种方法可以跨越shell调用“保留”$data?这个想法是我只读了一次XML文件。如果不是,是否有更简单的方法可以做到这一点?这些都是事我不能改变:

  • 配置文件是一个XML
  • 需要的变量在shell脚本
+1

请注意,Switch.pm已弃用。 :) – 2010-04-26 18:56:52

回答

5

当我需要一些资料,由Perl的检索,在一个shell脚本,我用Perl和集生成shell脚本通过eval环境变量:

myscript

#!/bin/bash 
BINDIR=`dirname $0` 
CONFIG=$BINDIR/config.xml 
eval `$BINDIR/readcfg $CONFIG` 
echo Running on the $planet near the $star. 

readcfg

#!/usr/bin/perl 
use XML::Simple; 
my $xml = XMLin('config.xml', VarAttr => 'name', ContentKey => '-content'); 
while (my ($k, $v) = each %{$xml->{param}}) { 
    $v =~ s/'/'"'"'/g; 
    $v = "'$v'"; 
    print "export $k=$v\n"; 
} 

​​3210

<config> 
    <param name="star">Sun</param> 
    <param name="planet">Earth</param> 
</config> 
+0

真的很好!谢谢 – Freddy 2010-04-26 21:00:18

+0

这每次都读取XML文件,这是我认为你不想做的。 – 2010-04-26 21:07:59

+0

不,您只读了一次XML,之后所有XML标记都只是shell变量,并且可以在shell脚本中使用,而无需再次调用readcfg。 – Freddy 2010-04-26 21:17:23

2

您可以分两步做到这一点。如果你还没有这样做,你想创建一个存储的Perl数据结构,并且当你有时,你存储的版本,所以你不必再解析它。

有五月的方式来做到这一点,但这里是一个使用Storable版本:

use Storable qw(nstore retrieve); 

my $stored_data_structure = 'some_file_name'; 

my $data = do { 
     # from the stored data structure if it is there 
     if(-e $stored_data_structure) { 
      retrieve($stored_data_structure); 
      } 
     # otherwise parse the xml and store it 
     else { 
      my $data = $xml->XMLin($xml_filename); 
      nstore($data, $stored_data_structure); 
      $data; 
      } 
     }; 

您也可以考虑反向你的概念。而不是调用你的Perl脚本一个shell脚本的,有你的Perl脚本调用你的shell脚本:

# load the data, as before 

# set some environment variables 
$ENV{SomeThing} = $data->{SomeThing}; 

# now that the environment is set up 
# turn into the shell script 
exec '/bin/bash', 'script_name' 
+0

谢谢,我测试过,它取得了诀窍。然而,在读取每个变量时,它将从存储的文件中读取。但是,谢谢,我不知道你可以在Perl中做到这一点。 – Freddy 2010-04-26 21:01:50

1

你可以简单的值存储在壳可执行文件。 假设的Bourne shell(SH)脚本,你事先知道的变量名兴趣列表你:

$data=""; 
sub loadXMLConfig() 
{ 
    $filename="somexml.xml" 
    $data = $xml->XMLin($filename); 
} 

sub GetVariable() 
{ 
    ($FriendlyName) = @_; 
    switch($FriendlyName) 
    { 
     case "My Friendly Name" { 
      print "$FriendlyName='$data->{my_xml_tag_name}'; export $FriendlyName\n" 
     } # Use "export var=value" form for bash 
     .... 
     .... 
     .... 
     } 
} 

sub storeVariables { 
    # @variables list defined elsewhere - this is just a sketch 
    foreach my $variable (@variables) { 
     GetVariable($variable); 
    } 
} 

然后调用如下:

$ perl -e 'require "scrpt.pl"; loadConfigFile(); storeVariables();' > my_vars.sh 
$ source my_vars.sh 

而且my_vars.sh可以采购很多次

+0

谢谢,它绝对会工作。然而,提出了一种更优雅的方式,它基本上在shell侧使用eval而不是加载文件来做同样的事情。 – Freddy 2010-04-26 21:02:55

相关问题