How can I extract and save values from an XML file in Perl?

Posted by Freddy on Stack Overflow See other posts from Stack Overflow or by Freddy
Published on 2010-04-26T18:43:54Z Indexed on 2010/04/26 18:53 UTC
Read the original article Hit count: 245

Filed under:
|
|

Here is what I am trying to do in a Perl script:

$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}}
         ....
         ....
         ....
      }
}

The problem is I am using Perl just because I am reading from an XML file, but I need to get these variables by a shell script. So, here is what I am using:

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

This works exactly as expected, but I need to read the XML file every time I am getting a variable. Is there a way I could "preserve" $data across shell calls? The idea is that I read the XML file only once. If no, is there is a more simple way I could do this? These are the things I can't change:

  • Config File is an XML
  • Need the variables in a shell script

© Stack Overflow or respective owner

Related posts about perl

Related posts about shell