Is this a bug of require of Perl 5.8.8?

2019-08-07 11: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.



标签: perl require