问题
In many perl scripts (especially in famous CPAN distros) I find the following piece of code:
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
if 0; # not running under some shell
I was wondering what it is for?
Thanks.
回答1:
Some systems don't recognize the #!/usr/bin/perl
line at the start of scripts, and so trying to invoke a Perl program by name on such a system will simply pass the script to the shell. In order to combat this, portable Perl programs begin with a line that, when interpreted by a standard POSIX shell, causes the script to be passed to perl(1)
instead. The if 0
causes the line to be ignored when run by Perl itself, and placing it on a separate line causes shells to treat it as a separate command, running just the eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
as soon as it's read.
回答2:
The line is valid under shells as well as perl. In perl eval
is skipped because of following if 0
. In shell, same file is executed in perl
using the eval.
来源:https://stackoverflow.com/questions/9143726/what-means-not-running-under-some-shell-in-perl-scripts