博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to change @INC to find Perl modules in non-standard locations
阅读量:5332 次
发布时间:2019-06-14

本文共 2062 字,大约阅读时间需要 6 分钟。

http://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations

问题:

自己写的或者下载安装的module没有放在standard location ,如a module called My::Module saved in /home/foobar/code/My/Module.pm.

如果一个Perl脚本使用此模块,直接use My::Module; 则报错:

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

解释:

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. 

解决:

1.PERLLIB5 (推荐)or PERLLIB

  ~/.bashrc 

  export PERL5LIB=/home/foobar/code(例子)

2. 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.

    use lib '/home/foobar/code';

    use My::Module;

3. -I on the command line

  temporary solution. 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:

 

  1. When you have a fixed, but not standard company-wide environment in which you put modules in a common standard location.
  2. 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://www.cnblogs.com/tina-ma/p/4312570.html

你可能感兴趣的文章
P1087-FBI树
查看>>
怎么在某个控制器中判断程序是否在前台或后台
查看>>
第三周vim入门学习1
查看>>
Linux内核分析(第九周)
查看>>
Serlvet学习笔记之一 ——实现servlet的3种方法
查看>>
批处理
查看>>
使用pycharm编写自动化脚本
查看>>
browser-sync启动命令
查看>>
HttpWebRequest请求返回非200的时候 HttpWebResponse怎么接受返回错误提示
查看>>
VBScript 内置函数
查看>>
java打jar包的几种方式详解
查看>>
关于sublime3中package controle不出来的问题
查看>>
groovy
查看>>
对象扩展
查看>>
js学习总结----事件基础
查看>>
7_20 day25 总结
查看>>
Fliter(过滤器)的认识
查看>>
sd 卡驱动--基于高通平台
查看>>
java开发中的那些事(6)------一次ajax调用中的问题
查看>>
34 数组中的逆序对+改进低效归并排序
查看>>