Perl,为什么@INC不同?

时间:2012-03-01 13:07:15

标签: perl library-path

我有一个简单的Perl脚本,打印出@INC如下:

#!/usr/bin/perl
print $_, "\n" for @INC;

我使用./test.plperl test.pl以两种不同的方式执行脚本,输出如下:

[neevek@~/bin]$ ./test.pl 
/Library/Perl/5.12/darwin-thread-multi-2level
/Library/Perl/5.12
/Network/Library/Perl/5.12/darwin-thread-multi-2level
/Network/Library/Perl/5.12
/Library/Perl/Updates/5.12.3
/System/Library/Perl/5.12/darwin-thread-multi-2level
/System/Library/Perl/5.12
/System/Library/Perl/Extras/5.12/darwin-thread-multi-2level
/System/Library/Perl/Extras/5.12
.   
[neevek@~/bin]$ perl test.pl 
/opt/local/lib/perl5/site_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/site_perl/5.12.3
/opt/local/lib/perl5/vendor_perl/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/vendor_perl/5.12.3
/opt/local/lib/perl5/5.12.3/darwin-multi-2level
/opt/local/lib/perl5/5.12.3
/opt/local/lib/perl5/site_perl
/opt/local/lib/perl5/vendor_perl
.   

我的问题是:使用./script.plperl script.pl执行perl脚本的幕后操作是什么?是什么导致脚本输出不同的@INC

2 个答案:

答案 0 :(得分:19)

脚本正在从perl通过shebang行执行/usr/bin,但是从命令行启动脚本使用来自perl的不同/opt/somewhere二进制文件(请参阅{ {1}}为路径)。您可以使用which perl使两个选项的行为相同。

答案 1 :(得分:10)

您的系统上安装了两个perl。

执行./test.pl时,会选择/usr/bin/perl,如脚本第一行所示。

执行perl test.pl时,它是在PATH环境变量中找到的第一个perl。输入which perl以发现它的位置。