Is it possible to manually include a CPAN library in a Perl script?

依然范特西╮ 提交于 2019-12-10 12:22:52

问题


The situation I am in is that I have written a perl script which uses the CPAN library XML::Simple and tested in an environment which has the ability to install the necessary CPAN library.

However the environment which the script must be run in does not have the permissions to install CPAN libraries. I was wondering if there is some manual way to include the Simple.pm file with my script and include it in the directory with the script? I have tried using 'use' and 'require' to no avail.

Any help is greatly appreciated.

edit: I was able to solve the problem by manually downloading the Simple.pm file and using the line require "Simple.pm"; in my code. I think I must have downloaded the incorrect file somehow when I tried this orginally. Thanks.


回答1:


Since XML::Simple is a pure Perl module, you could download the package, extract and use it. You will have to use the lib pragma to specify the directory containing the module.

Suppose your directory structure is (only relevant files have been included):

my_app.pl
XML-Simple
XML-Simple/lib
XML-Simple/lib/XML
XML-Simple/lib/XML/Simple.pm

Your my_app.pl will have access to XML::Simple by including the following code:

use lib 'XML-Simple/lib';
use XML::Simple;



回答2:


You can certainly include XML::Simple itself with you module, but then XML::Simple itself depends on other modules. It needs either XML::Parser (which depends on libexpat being around) or XML::SAX. XML::SAX includes XML::SAX::PurePerl, a slow (and incomplete the last time I checked a long time ago) pure-perl parser. Alternatively XML::SAX can use XML::LibXML (depends on libxml2) or XML::SAX::Expat

Bottom line: if you want your application to be self-contained, you need to include at least XML::Simple and XML::SAX, and past a certain volume of data to process, you probably need also a C parsing library.




回答3:


I was able to solve the problem by manually downloading the Simple.pm file and using the line require "Simple.pm"; in my code. I think I must have downloaded the incorrect file somehow when I tried this orginally.




回答4:


Can you manually download the CPAN module on another machine and install it on the target environment? http://www.thegeekstuff.com/2008/09/how-to-install-perl-modules-manually-and-using-cpan-command/




回答5:


I do something like this:

use FindBin;

BEGIN {
  unshift @INC $FindBin::Bin
}

Then

use XML::Simple;

should just work and it enables you to release the package with your script.



来源:https://stackoverflow.com/questions/4214047/is-it-possible-to-manually-include-a-cpan-library-in-a-perl-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!