Perl functions to find nodes and get child nodes in ActivePerl

541 Views Asked by At

I have a Perl program to parse an input XML file and print values to an output text file.

It seems to work fine when I build with the DWIM Perl GUI, but when I build the same with ActivePerl it fails due to missing libraries.

Can someone help me converting the code to the ActivePerl equivalent?

The Perl code is

use strict;
use warnings;

use XML::LibXML;

open( my $File, '>', 'options.txt' );

my $filename = $ARGV[0];

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file( $filename );

for my $sample ( $xmldoc->findnodes( '/Releases/Release' ) ) {

    foreach my $child ( $sample->getChildnodes ) {

        if ( $child->nodeType() == XML_ELEMENT_NODE ) {
            print $File $child->textContent(), " ";
        }
    }

    print $File "\n";
}

My Input would be like (in XML)

<!-- Pre-Seed File for Script Generation -->
<Releases>
  <Release>
    <Family>F1L</Family>
    <ArVersion>3.2.2</ArVersion>
    <ReleaseVersion>C3.03.10.001</ReleaseVersion>
    <PackageType>FULL</PackageType>
  </Release>
  <Release>
    <Family>F1H</Family>
    <ArVersion>4.0.3</ArVersion>
    <ReleaseVersion>Ver4.03.20</ReleaseVersion>
    <PackageType>SPAL</PackageType>
  </Release>
</Releases>

The output should be

F1L 3.2.2 C3.03.10.001 FULL 
F1H 4.0.3 Ver4.03.20 SPAL 
3

There are 3 best solutions below

0
Dave Cross On

There is no need for an "Activeperl equivalent". Perl code is cross-platform. Your code will work perfectly well under Active Perl. You just need to install the missing libraries. And there are plenty of answers on this site which can help you with that.

1
Borodin On

"I have an activeperl license" You don't need an ActivePerl license—Perl is open source and free, unless you've forked out for the $1,000 per year "Business Edition"

You should either install XML::LibXML on your ActivePerl or you should carry on using DWIMPerl (which is basically Strawberry Perl with a few extra modules installed). If you're using ActiveState's Komodo then it should work fine with any current Perl

0
ikegami On

ActivePerl is not a language; it's a Perl distribution. The code will run fine on ActivePerl as is.

>perl -v | perl -ne"print if /This|provided/"
This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread
Binary build 1603 [296746] provided by ActiveState http://www.ActiveState.com

>perl a.pl a.xml

>type options.txt
F1L 3.2.2 C3.03.10.001 FULL
F1H 4.0.3 Ver4.03.20 SPAL