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

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 that I only use Perl because I am reading from an XML file, but I need to get these variables with a shell script. So here is what I use:

$ 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 get a variable. Is there a way to "save" $data through shell calls? The idea is that I only read the XML file once. If not, is there an easier way to do this? This is what I cannot change:

  • Config file is XML
  • Need variables in the shell script
+4
source share
3 answers

When I need the information received by Perl in a shell script, I create a Perl script shell and set the environment variables via 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"; } 

config.xml

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

You can do this in two steps. You want to create a saved Perl data structure if you havenโ€™t done it yet, and when you have it, you saved the version so you don't have to parse it again.

Is there a way to do this, but here is the version using 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; } }; 

You can also consider reversing your concept. Instead of a shell script that calls your Perl script, ask your Perl script to call your shell script:

  # 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' 
+2
source

You can simply save the values โ€‹โ€‹in an executable shell file. assuming a bourne shell (sh) script and that you know in advance the list of variable names you are interested in:

 $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); } } 

And then call the following:

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

And my_vars.sh can be sent many times

+1
source

Source: https://habr.com/ru/post/1308058/


All Articles