Is this a bug of require of Perl 5.8.8?

依然范特西╮ 提交于 2019-12-11 07:41:36

问题


Code as follows:

#!/usr/bin/perl

print 1;
{
require shift;                                                                        
}

It turns out that when I run ./script script,two 1s are printed.

So my script file is actually loaded twice? Is this a bug or not??

According to the document:

"require" demands that a library file be included if it hasn’t already been included.


回答1:


require only skips loading a file if it was previously loaded with require, use, or do, not for something loaded as the primary script.

See: %INC




回答2:


No, it's not a bug. Your script prints 1, then requires the name of the first argument, in this case you've supplied the name 'script' which runs your script again printing the second 1.

See Require;

The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements.

Also you should see some error, such as Null filename used at script line 5




回答3:


When I run your code i get:

$ ./script script
Null filename used at script line 5.
Compilation failed in require at ./script line 5.
11$

So I can't reproduce your result... ;)

You're making a false assumption: the fact that your script is called 'script' has nothing to do with it being used, required on done... the require statement is the first time something is required.



来源:https://stackoverflow.com/questions/6899556/is-this-a-bug-of-require-of-perl-5-8-8

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