Background:
A perl script need to be run on various servers (all AIX unix). The perl script need the data from a xml script. Just read the data, no write or other modify action. The contents of xml script changes sometimes.
Problem:
I can not use xml parser module (like XML::LibXML) in the perl script, since the aix server does not install the module, and I don't want to install them one by one. There are many aix servers.
There is one linux server(red hat linux) that installed the xml module. And every aix server can access it.
Any way to pass the xml data to the perl script, while do not require the xml module? Like first parse the xml on the linux, then pass the output as input to the perl script? Don't know if it's workable.
If the XML is not complex, you can write a simple script that convert it to other format. such as simple hash, use Data::Dumper to dump it to a file, and eval the output on your AIX.
If it is complex, you need to make your own in-memory representation of the XML, (with noting attributes, child elements, text nodes, namespaces... how complex is your input?) and dump / eval that.
sample code to evaling: (untested)
# read the data to variable
open my $fh, "<", "dumpfile.txt" or die "file not found";
my $dump = do { local $/ = <$fh> };
close $fh;
# eval the dump
my $VAR1;
eval $dump;
If you have a .xsd schema for the XML data available (having a schema is always a good idea), then you can use XML::Compile
to transform the XML data into a perl data structure. XML::Compile
also comes with a convenience script xml2yaml
to create a YAML representation.
Another popular module for transforming XML data into perl data structures is XML::Simple
. Unfortunately, as XML::Simple
does not have a schema available, it has often to guess how to create the data structure (i.e. if it's about to create a scalar value or an array value), leading to surprises. The documentation of XML::Simple
explicitly says that the use of this module is discouraged.
Once you have the data structure you only need to decide on the serialization format (YAML, JSON, Storable, Data::Dumper...) and transfer the data to the other systems.
Creating an in-memory representation that's reasonably future-proof is going to be a lot of work and a lot frustrating debugging sessions, @Slaven's answer is a better solution.
Just to give you another option, you could also: download a no-dependency CPAN module like https://metacpan.org/pod/XML::TinyXML or https://metacpan.org/pod/XML::Tiny anywhere, and copy it to the AIX machines when you copy your own script there. For eg., you could just put the XML/TinyXML.pm file in a folder along with your script, and use it from your script using FindBin, then copy the folder as a whole to the AIX machines.