简体   繁体   中英

not able to run perl script, use command not found?

I have an xml file (client_23.xml) in which I need to add few lines at a particular section basis on what is there in other xml file (abc_lop.xml) .

Here is my abc_lop.xml file in which you see I have lot of ClientField lines with name , pptype and dataType in it.

<Hello version="100">
 <DataHolder numberOfFields="67">
  <ClientField name="target" pptype="aligning" dataType="string">
   <Value value="panel_3646"/>
   <Value value="panel_3653"/>
  </ClientField>
  <ClientField name="category_3652_0_count" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.age" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.gender" pptype="aligning" dataType="string">
   <Value value=""/>
   <Value value="F   "/>
   <Value value="NA"/>
  </ClientField>
 </DataHolder>
</Hello>

I need to read this file and extract name from those ClientField lines and if its pptype is not aligning then I need to construct this line for each name: Below is an example for two names, only first value is different and apart from that other two will always be same.

<eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
<eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>

Now if its pptype is aligning , then construct line like this:

<eval>upsert("target", "NA", $calty_strings)</eval>
<eval>upsert("pme.gender", "NA", $calty_strings)</eval>

And all these changes I have to make in client_23.xml file and then make a new file with it so my new file will look like this as an example: I will have a function with name data_values in which I need to add above stuff in the <block> tag as shown below.

    <function>
        <name>data_values</name>
        <variables>
            <variable>
            <name>temp</name>
            <type>double</type>
            </variable>
        </variables>
        <block>
            <eval>temp = 1</eval>
            <eval>upsert("category_3652_0_count", 0, $calty_doubles)</eval>
            <eval>upsert("category_3652_2_count", 0, $calty_doubles)</eval>
            <eval>upsert("target", "NA", $calty_strings)</eval>
            <eval>upsert("pme.gender", "NA", $calty_strings)</eval>             
        </block>
    </function>

This is what I have currently in client_23.xml file so after adding, it will look like above:

    <function>
        <name>data_values</name>
        <variables>
            <variable>
            <name>temp</name>
            <type>double</type>
            </variable>
        </variables>
        <block>
            <eval>temp = 1</eval>
        </block>
    </function>

I came up with below perl script but whenever I try to run it, it doesn't run and I am seeing some errors:

#!/usr/bin/perl
use strict;
use warnings;

use XML::LibXML;

# Open the main XML file and locate the
# <block> element that we need to insert into
#
my $doc = XML::LibXML->load_xml(
    location => 'client_23.xml',
    no_blanks => 1,
);
my $block = $doc->find('/function/block')->get_node(1);

# Open the secondary XML file and find all the <ClientField> elements
# that contain the data we need to insert
#
my $abc = XML::LibXML->load_xml(location => 'abc_lop.xml');

for my $field ( $abc->find('/Hello/DataHolder/ClientField')->get_nodelist ) {

    my ($name, $pptype) = map $field->getAttribute($_), qw/ name pptype /;

    my $text = $pptype eq 'aligning' ?
        sprintf q{upsert("%s", "NA", $calty_strings)}, $name :
        sprintf q{upsert("%s", 0, $calty_doubles)}, $name;

    $block->appendTextChild('eval' , $text);
}

print $doc->toString(2);

I made perl script executable and ran it like this and I got an error. What's wrong?

./tester.perl: line 1: use: command not found
./tester.perl: line 2: use: command not found
./tester.perl: line 4: use: command not found
./tester.perl: line 9: syntax error near unexpected token `('
./tester.perl: line 9: `my $doc = XML::LibXML->load_xml('

Update:-

Use error is fixed now. I am seeing this error:

Can't locate XML/LibXML.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at ./tester.perl line 5.
BEGIN failed--compilation aborted at ./tester.perl line 5.

Do I need to install anything?

Can't locate XML/LibXML.pm 

So you need to install the XML::LibXML module. Try one of these commands.

If you have sudo access, and are on Debian Linux or Ubuntu, run this:

sudo apt-get install libxml-perl

If you have sudo access and are on Red Hat, CentOS, Fedora, or Amazon Linux:

sudo yum install perl-XML-LibXML

On other platforms with sudo (eg OS X), this should work, but you'll have to answer a lot of questions if you haven't run cpan before:

sudo cpan XML::LibXML

If you don't have sudo access, run the same command without sudo and select local::lib when it asks what approach you wish to take:

cpan XML::LibXML

Instead of calling the script by its name run:

perl /path/to/perl/script

I am wondering if you have a hidden character in your first line. Did you write the file in DOS?

With your update you will have to install the Perl Module it cannot find. There are instructions on CPAN on how to install additional modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM