Reputation: 6031
Code as follows:
#!/usr/bin/perl
print 1;
{
require shift;
}
It turns out that when I run ./script script
,two 1
s 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.
Upvotes: 1
Views: 165
Reputation: 98423
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
Upvotes: 4
Reputation: 3508
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 use
d, require
d on do
ne... the require
statement is the first time something is required.
Upvotes: 0
Reputation: 4503
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
Upvotes: 3