编译出现错误:Can't locate XML/Parser.pm in @INC

转载:How to change @INC to find Perl modules in non-standard locations

由于遇到了perl的编译问题,转载别人的文章,记录下来一一尝试


Loading your private Perl module

You have a script and have just started to move some parts of it, out to a new module called My::Module. You saved the module to /home/foobar/code/My/Module.pm.

Your perl script now starts like this:

use strict;

use warnings;

use My::Module;

When you run the script you get a friendly error message like this:

Can't locate My/Module.pm in @INC (@INC contains:

  /home/foobar/perl5/lib/perl5/x86_64-linux-gnu-thread-multi

  /home/foobar/perl5/lib/perl5

  /etc/perl

  /usr/local/lib/perl/5.12.4

  /usr/local/share/perl/5.12.4

  /usr/lib/perl5 /usr/share/perl5

  /usr/lib/perl/5.12

  /usr/share/perl/5.12

  /usr/local/lib/site_perl

  .).

  BEGIN failed--compilation aborted.

Perl cannot find your module.

Upgrading a Perl module

On a system you are thinking to upgrade a module that came from CPAN. You don't want to install it in the standard location yet. First you'd like to put it in some private directory, try it, and install to the system only once you are sure it works well.

In this case too you "install" the module in a private directory, e.g. in /home/foobar/code and somehow you'd like to convince perl to find that version of the module, and not the one that was installed in the system.

The use statement

When perl encounters use My::Module; it goes over the elements of the built-in @INC array that contains directory names. In each directory it checks if there is a subdirectory called "My" and if in that subdirectory there is a file called "Module.pm".

The first such file it encounters will be loaded into memory.

If it does not find the file you get the above error messages.

@INC is defined when perl is compiled and it is embedded in the binary code. You cannot change that, unless you recompile perl. Not something we would do every day.

Luckily the @INC array can be changed in several ways when we execute a script. We'll see these solutions and discuss when each one of them is appropriate to use.

PERLLIB and PERL5LIB

You can define the PERL5LIB environment variable (though PERLLIB works the same way, I'd recommend using PERL5LIB over PERLLIB as that makes it clear it is related to Perl 5) the same way you can define the PATH environment variable. Every directory listed in this variable will be added to the beginning of @INC.


On Linux/Unix when using Bash, you would write

export PERL5LIB=/home/foobar/code

You can add this to the ~/.bashrc to make it always available when you log-in.

On Windows you can set the same in the cmd command window by typing

set PERL5LIB=c:\path\to\dir


For a more long term solution follow these steps:

Right-click My Computer and click Properties.

In the System Properties window, click on the Advanced tab.

In the Advanced section, click the Environment Variables button.

In the Environment Variables window in the "User variables for Foo Bar" section click on New and type in the following:

Variable name: PERL5LIB

Variable value: c:\path\to\dir

Then click OK 3 times. Windows that you open after this will already know about the new variable. Type this in the command window, to see the newly set value:

echo %PERL5LIB%

This will add the private /home/foobar/code directory (or c:\path\to\dir directory) to the beginning of @INC for every script that is executed in the same environment.


In taint mode, that will be explained in a separate post, the PERLLIB and PERL5LIB environment variables are ignored.

use lib

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.

You just have to make sure to have the use lib statement before trying to load the module:

use lib '/home/foobar/code';

use My::Module;

One note here. I saw several companies where use lib statements were added to the modules so they will be able to load their dependencies. I don't think this is good. I think the right place to change @INC is the main script or even better, outside the script such as in the two other solutions.

-I on the command line

(That's a capital i)


The last solution is the most temporary solution. Add a -I /home/foobar/code flag to perl when running the script.

perl -I /home/foobar/code script.pl

This will add /home/foobar/code to the beginning of @INC for this specific execution of the script.

So which one to use?

If you would like to just test a newer version of a module, I'd recommend the command line flag: perl -I /path/to/lib.

If you are installing lots of modules in a private directory then I'd probably use PERL5LIB though we'll also see local::lib that does this for you.

use lib is used in two cases:How to change @INC to find Perl modules in non-standard locations

When you have a fixed, but not standard company-wide environment in which you put modules in a common standard location.

When you are developing an application and you'd like to make sure the script always picks up the modules relative to their own location. We'll discuss this in another post.

原文链接:https://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,312评论 0 10
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,694评论 0 3
  • 毎天清晨醒来,她都要静静地坐一会儿,有些想赖床,还有一点就是让自己从深睡状态中清醒过来,再静静地思考一下,似乎总是...
    余梦人生阅读 321评论 0 0
  • 晚睡晚起,不妥不妥。 A4纸书写。学习英语。 早上喝水莫名其妙杯子从中间断开,啪地碎地上……碎碎(岁)平安?! 吃...
    WANGRUIXUE阅读 394评论 0 1
  • 绘本《勇敢做自己》推荐阅读的十大理由 31 绚丽的色彩:给孩子以丰富的视觉刺激。 2 形象的方向展示,让孩子了解左...
    小小经典绘本书评阅读 14,620评论 0 2