alex alex - 1 year ago 94
Perl Question

'use lib' can't find Perl module

I'm on Windows 7, I downloaded a CPAN module called

, and I want to use it in a script I wrote.

I cannot modify
on my machine, I cannot modify environmental variables on my machine, I cannot run
on my machine, and I cannot use a package manager on my machine.

So in order to use the module, I went to the CPAN website, downloaded the module's
file, and unzipped it into a
folder in my project. I did that because this guide suggested that I can use
use lib
in the context of my script to reference the downloaded module:

Adding a use lib statement to the script will add the directory to @INC for that specific script. Regardless who and in what environment runs it.

My script is called
. I am trying to use the
module to parse an XML file called
. Here is an example of my directory structure:

→ sandbox/
→ lib/
→ XML-XPath-1.13/
→ XPath/

This is my

use strict;
use warnings;
use lib qw("C:/sandbox/lib/XML-XPath-1.13");

my $xml_file = "C:/sandbox/test.xml";
my $result;

if (-f $xml_file) {
$result = XML::XPath->new(filename => "$xml_file");

When I run this script with
, the script fails with the following error:

Can't locate object method "new" via package "XML::XPath" (perhaps you forgot to load "XML::XPath"?) at line 10.

Can I resolve this error with
use lib


When I add
use XML::XPath;
to my
script, as such:

use strict;
use warnings;
use lib qw("C:/sandbox/lib/XML-XPath-1.13");

I get an error like:

Can't locate XML/ in @INC

But as stated above, I cannot physically modify
or recompile Perl on my machine.

Answer Source

This is not going to work. XML::XPath depends on the XML::Parser module which needs to be compiled. Since you do not have a complete toolchain with a C compiler available, there is no way you are getting this to run. You will have to search for a different way to handle your XML.

Just extracting a tarball only works when you have a pure-Perl module.

The easiest way to get a full Perl toolchain on Windows is to install Linux install the Strawberry Perl distribution. There's even a ZIP edition and a portable edition that can be installed on a thumb drive, no admin privileges required.

Your more immediate problem is that XML::XPath in version 1.13 seems to be a shoddy module with an unusual directory layout. The package you are trying to load is at XML-XPath-1.13/ but should be at XML-XPath-1.13/lib/XML/ Why?

When you say use XML::XPath; this does two things. First, Perl searches for a file XML/ in all @INC directories. Assuming you have something like

use FindBin;
use lib "$FindBin::Bin/lib/XML-XPath-1.13";

then perl would expect a file PROJECT/lib/XML-XPath-1.13/XML/ which doesn't exist (where I'm using PROJECT as placeholder for the directory of your script).

Since the file PROJECT/lib/XML-XPath-1.13/ does exist you could say use XPath;.

But then the second thing happens: The package is imported so that it can load subroutines or constants into your namespace. The use'd package name is now used as a class: XPath->import. However, the file does not contain the XPath package but the XML::XPath package, so there is no XPath class with an import method.

The import method call can be suppressed by providing an empty list in the use statement: use XPath ();.

Better than such hacks, you could use a more recent version of XML::XPath (version 1.13 is from 2003, but the newer version 1.42 is from 2007). Once you extract it you could use lib "$FindBin::Bin/lib/XML-XPath-1.42/lib" and then use XML::XPath;.

But this still won't work, because XML::XPath does use XML::Parser and you don't have that module installed. Now we are back to your main problem: XML::Parser needs to be compiled first because it contains a C extension, and also relies on an external library. Without a full toolchain you can't compile it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download