gpt4 book ai didi

Doesn't Perl include current directory in @INC by default?(默认情况下,Perl不包括@Inc中的当前目录吗?)

转载 作者:bug小助手 更新时间:2023-10-28 20:45:41 25 4
gpt4 key购买 nike



I have never fully understood Perl's resolution of package names, but I always assumed that the following should always work, assuming you are executing myscript.pl from within the directory that contains it:

我从来没有完全理解过Perl对包名的解析,但我总是假设以下内容应该总是有效的,假设您是从包含它的目录中执行myscript.pl:



myscript.pl (contains the following statement: use Class1::Class2::Class3)
Class1/
Class2/
Class3.pm (contains the following package declaration: package Class1::Class2::Class3;)


However, this is not working in my code because Class3.pm cannot be located. Looking at @INC, it does not include the current directory, only various directories of my Strawberry Perl installation.

然而,这在我的代码中不起作用,因为找不到Class3.pm。查看@Inc,它不包括当前目录,只包括我的Strawberry Perl安装的各种目录。



What is the recommended way to solve this? I suppose I could modify @INC, or I could start using FindBin, but I'm not sure which is best. I have inherited this code and am simply migrating it to a new location, but it doesn't look like the old code needed either such solution (I could be wrong, still looking...)

解决此问题的推荐方法是什么?我想我可以修改@Inc,或者我可以开始使用FindBin,但我不确定哪个是最好的。我继承了这段代码,只是简单地将它迁移到一个新位置,但看起来旧的代码不需要这样的解决方案(我可能是错的,还在寻找……)


更多回答

v5.26 removes dot from @INC

v5.26删除@INC中的点

优秀答案推荐

Perl has never searched the script's directory for modules.

Perl从未在脚本的目录中搜索模块。


Perl did search the current directory. It is because the current directory is sometimes the same as the script directory that it appeared that Perl searched the script's directory.

Perl确实搜索了当前目录。因为当前目录有时与脚本目录相同,所以Perl似乎搜索了脚本的目录。


But the current directory is often different than the script's directory. So code like yours that assumes that Perl searches the script's directory for modules has always been buggy.

但当前目录通常与脚本的目录不同。因此,像您这样假设Perl在脚本目录中搜索模块的代码总是有错误的。


Since 5.26, Perl no longer searches the current directory for security reasons. This simply made the bug more evident.

从5.26开始,出于安全原因,Perl不再搜索当前目录。这只会让这个漏洞变得更加明显。




To tell Perl to look in the script's directory for modules, use the following:

要告诉Perl在脚本目录中查找模块,请使用以下命令:


use FindBin 1.51 qw( $RealBin );
use lib $RealBin;

or


use Cwd qw( abs_path );
use File::Basename qw( dirname );
use lib dirname( abs_path( $0 ) );


Having . (the current directory) in @INC was removed in 5.26 for security reasons (CVE-2016-1238). Some Linux distributions have backported the change, so you might run into this problem even if you're using e.g. 5.24.

拥有。出于安全原因,@Inc中的(当前目录)在5.26中被删除(CVE-2016-1238)。一些Linux发行版支持这一更改,所以即使您使用的是5.24,也可能会遇到这个问题。



Perl 5.26 removed having the current working directory in @INC as a security measure.

作为安全措施,Perl 5.26删除了将当前工作目录放在@Inc中。



It's explained in the 5.26 perldelta notes.

这在5.26的每三角音符中有解释。


更多回答

A tangential question, but why $RealBin and not just $Bin? Does having the links resolved give us any benefit here, or have you used it here just a general good practice?

一个离题的问题,但为什么是$RealBin,而不仅仅是$Bin?解决链接对我们在这里有什么好处吗,或者您在这里使用它只是一种普遍的良好做法?

@sundar, $Bin won't work if someone creates a symlink to the script.

@sundar,如果有人创建了指向脚本的符号链接,$Bin将不起作用。

What is the 1.51 argument to FindBin? I don't see anything like that usage mentioned in perldoc for FindBin.

FindBin的1.51论点是什么?我没有看到任何类似PerldocFindBin中提到的用法。

@Greg Kennedy, It's a version check. See the use Module VERSION LIST syntax of use.

@格雷格·肯尼迪,这是一个版本检查。请参阅Use的Use模块版本列表语法。

@textral, No difference between __FILE__ and $0 (unless you changed $0, so __FILE__ is slightly better). That's not the problem. The problem is that you used realpath(dirname(__FILE__)) instead of dirname(realpath(__FILE__)). Your way will fail if a symlink is used.

@tExtral,__FILE__和$0之间没有区别(除非您更改了$0,所以__FILE__稍好一些)。这不是问题所在。问题是您使用的是realpath(dirname(__File__)),而不是dirname(realpath(__File__))。如果使用符号链接,您的方法将失败。

@ikegami Oh, interesting. I didn't know FindBin was fixed. It did search $PATH until version 1.51, released with perl 5.16.

@池上哦,很有趣。我不知道FindBin已经修好了。它确实搜索了$PATH,直到与Perl 5.16一起发布的1.51版。

@ikegami I've retracted my code. Your FindBin solution is better (assuming it's at least version 1.51, so use FindBin 1.51 qw($RealBin) may be a good idea).

@池上,我收回了我的代码。你的FindBin解决方案更好(假设它至少是1.51版本,所以使用FindBin 1.51 QW($RealBin)可能是个好主意)。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com