What means “not running under some shell” in Perl scripts?

孤人 提交于 2019-12-10 13:47:00

问题


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

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