4

I am trying to replicate what my C#/XPath code does on Linux using Perl. I copied and pasted the code in Example 8-6 in Perl & XML. If I understand right, I should be able to run that Perl code, put this code in terminal

xmlPerl.pl mydatafile.xml "/inventory/category/item/name"

But when I try to run the Perl file, it doesn't work. Here is the error:

[root@Perl ~]# perl xmlPerl.pl
Can't locate XML/XPath.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at xmlPerl.pl line 3.
BEGIN failed--compilation aborted at xmlPerl.pl line 3.

What am I doing wrong? I think it has something to do with the XML and XPath names in the beginning of my code. Do I need to install something to use the XPath framework? I am running on RedHat 5.5.

daxim
  • 39,270
  • 4
  • 65
  • 132
PolarisUser
  • 719
  • 2
  • 7
  • 18
  • From the [Stack Overflow Perl FAQ](http://stackoverflow.com/questions/tagged/perl?sort=faq): [What's the easiest way to install a missing Perl module?](http://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module) – daxim Jun 25 '12 at 16:16
  • Try to get a modern book that teaches [LibXML](http://www.xmlsoft.org/) instead, see [What is the easiest way to do XPath querying of XML data in Perl?](http://stackoverflow.com/questions/5275610/what-is-the-easiest-way-to-do-xpath-querying-of-xml-data-in-perl) – daxim Jun 25 '12 at 16:20

3 Answers3

9

From perldiag:

Can't locate %s

You said to do (or require, or use) a file that couldn't be found. Perl looks for the file in all the locations mentioned in @INC, unless the file name included the full path to the file. Perhaps you need to set the PERL5LIB or PERL5OPT environment variable to say where the extra library is, or maybe the script needs to add the library name to @INC. Or maybe you just misspelled the name of the file. See require in perlfunc and lib.

You don't have installed XML::XPath module, or Perl not found it. Install module with CPAN:

> cpan XML::XPath

or with package manager:

> apt-get install libxml-xpath-perl

Or if it already installed say where it is with PERL5LIB environment variable:

> PERL5LIB=/path/to/lib perl ...

@INC variable:

BEGIN {
    unshift(@INC, '/path/to/lib');
}

or lib pragma:

use lib '/path/to/lib';
Community
  • 1
  • 1
Denis Ibaev
  • 2,470
  • 23
  • 29
  • That worked, it installed and everything, but now I am getting this error - "No path to find at /usr/lib/per5/site_perl/5.8.8/XML/XPath.pm line 65" I also installed it using cpan. – PolarisUser Jun 25 '12 at 16:32
3

That's the standard error that comes from trying to use a module that isn't installed. You should install it.

Ideally use the OS package for it; for example on a debian-derived OS (such as Debian or Ubuntu)

$ apt-get install libxml-xpath-perl

Failing that, you can install it as usual using CPAN

$ cpan XML::XPath
LeoNerd
  • 8,344
  • 1
  • 29
  • 36
  • Ah I see. I am running it on RedHat 5.5 I will try and see if I can find an install for it. I will post back on it that solution worked! Thank you! – PolarisUser Jun 25 '12 at 16:17
  • That worked, it installed and everything, but now I am getting this error - "No path to find at /usr/lib/per5/site_perl/5.8.8/XML/XPath.pm line 65" I also installed it using cpan. – PolarisUser Jun 25 '12 at 16:33
  • Well, the `No path to find` error is most likely an actual XPath error, complaining that it can't find the requested node in your data structure. That doesn't sound like a perl error. – LeoNerd Jun 25 '12 at 16:49
0

The answer is in the first part of your error:

Can't locate XML/XPath.pm

In Perl, the huge benefit is from using modules, or libraries, that others have written for you and you can reuse. In this case, someone has written a module called XML::XPath (in Perl, the path is delineated by '::') and you just need to install it. The easiest way to install it is via cpan, it's a tool that comes installed with most Perl installations. Just run:

cpan
(you'll be dropped into a different command prompt)
install XML::XPath

This will go out and fetch XML::XPath, unpack it, generate the Makefile, check for dependencies (and install any that are missing), make it, test it, and install it for you. Look here for more information on using CPAN.

Joel
  • 3,435
  • 2
  • 23
  • 33
  • That worked, it installed and everything, but now I am getting this error - "No path to find at /usr/lib/per5/site_perl/5.8.8/XML/XPath.pm line 65" I also installed it using cpan. – PolarisUser Jun 25 '12 at 16:32