-I switches on shebang line

X

xhoster

I'm sure something in perldoc perlrun is relevant to my question, but
I can't figure out what. Please give me a kick in the pants.

$ cat foo.pl
#!/home/xho/perl_10/bin/perl -Ifoo -Ibar -l
print foreach @INC;
__END__

$ ./foo.pl
foo -Ibar -l
/home/xho/perl_10/lib/5.10.0/x86_64-linux-thread-multi
/home/xho/perl_10/lib/5.10.0
/home/xho/perl_10/lib/site_perl/5.10.0/x86_64-linux-thread-multi
/home/xho/perl_10/lib/site_perl/5.10.0
..
foo
bar

So, why are foo and bar put at the end of @INC, instead of at the beginning
where perlrun says they should?

And why is "foo -Ibar -l" put at the front of @INC, as if all of the
shebang line after the first word were put in quotes?

This behavior doesn't seem specific to 5.10.0, it is seen in various 5.8
as well. If invoked like "perl foo.pl" then the foo and bar still
go at the end, rather than beginning, but the weird first entry is omitted.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
B

Ben Morrow

Quoth (e-mail address removed):
I'm sure something in perldoc perlrun is relevant to my question, but
I can't figure out what. Please give me a kick in the pants.

$ cat foo.pl
#!/home/xho/perl_10/bin/perl -Ifoo -Ibar -l
print foreach @INC;
__END__

$ ./foo.pl
foo -Ibar -l
/home/xho/perl_10/lib/5.10.0/x86_64-linux-thread-multi
/home/xho/perl_10/lib/5.10.0
/home/xho/perl_10/lib/site_perl/5.10.0/x86_64-linux-thread-multi
/home/xho/perl_10/lib/site_perl/5.10.0
.
foo
bar

So, why are foo and bar put at the end of @INC, instead of at the beginning
where perlrun says they should?

And why is "foo -Ibar -l" put at the front of @INC, as if all of the
shebang line after the first word were put in quotes?

This behavior doesn't seem specific to 5.10.0, it is seen in various 5.8
as well. If invoked like "perl foo.pl" then the foo and bar still
go at the end, rather than beginning, but the weird first entry is omitted.

This is all to do with the way #! lines are parsed by the OS, and by
perl. The OS treats everything after the first space as a single
argument (to avoid needing to parse quoting in the kernel), and
effectively invokes perl as

/home/xho/perl_10/bin/perl "-Ifoo -Ibar -l" foo.pl

perl prepends "foo -Ibar -l" to @INC, as documented, then constructs its
default @INC. Then it finds the #! line itself, parses it for more
options, and any -I found then go at the *end* of @INC. I don't know if
this is documented anywhere, but it happens at least as far back as 5.6.

In simple terms: #! parsing is highly dodgy, and perl's efforts to make
it less so don't always help. I would 'use lib', instead.

Ben
 
E

Eric Pozharski

Ben Morrow said:
Quoth (e-mail address removed): *SKIP*
This is all to do with the way #! lines are parsed by the OS, and by
perl. The OS treats everything after the first space as a single
argument (to avoid needing to parse quoting in the kernel), and
effectively invokes perl as
/home/xho/perl_10/bin/perl "-Ifoo -Ibar -l" foo.pl

[a bit of explanation for unwary reader] Please note that Ben talks
about OS. When at command line you type C<perl -Ifoo -Ibar whatever>
that's shell that parses. In case C<./foo.pl> after some usual
maintanance shell forks-execs and then that's the kernel that looks what
would eat that text. The rest you know.
perl prepends "foo -Ibar -l" to @INC, as documented, then constructs its
default @INC. Then it finds the #! line itself, parses it for more
options, and any -I found then go at the *end* of @INC. I don't know if
this is documented anywhere, but it happens at least as far back as 5.6.
In simple terms: #! parsing is highly dodgy, and perl's efforts to make
it less so don't always help. I would 'use lib', instead.

BTW, it should be a (the?) problem for any interpreter. Thanks for
explanation. Personally I'll be very careful tinkering with shebang.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top