What is the exact meaning of the find2perl perl shebang + eval?

穿精又带淫゛_ 提交于 2019-12-19 04:07:05

问题


What exacly do the following?

#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell
  • the if 0 is never true, so the eval part will never executed,
  • and the eval is strange too - what is the value of $0 in this context (inside single quotes?)

Ps: taken from the result of the find2perl command


回答1:


Best guess - as in this comment #$running_under_some_shell, it's to detect if the script is being run by some shell other than perl, e.g. bash.

the if 0 is never true, so the eval part will never executed,

Not by perl, no. By other shells such as bash it won't spot the line continuation and will just execute the eval statement. This then re-runs the script under perl. (Oddly with different options than the hashbang line.)

and the eval is strange too - what is the value of $0 in this context (inside single quotes?)

Again, this will be expanded by bash not perl: here it means the path to find2perl to pass into the perl interpreter.




回答2:


I found some discussion here: http://www.perlmonks.org/?node_id=825147

The extended hashbang is there so you can run your Perl script with almost any /bin/sh under the sun, even a shell/kernel that does not honor the hashbang and it will still launch perl in the end.



来源:https://stackoverflow.com/questions/6123971/what-is-the-exact-meaning-of-the-find2perl-perl-shebang-eval

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