Perl 根据 shebang 行派遣给其他口译员?
Perl dispatches to other interpreters based on shebang line?
当我不小心 运行 一个使用 perl 的 bash 脚本并且它......有效时,我的想法被震撼了。进一步试验,似乎 perl 读取脚本的 shebang 并发送给正确的解释器:
$ cat /tmp/ohhai.sh
#!/bin/bash
echo ohhai bash
$ perl /tmp/ohhai.sh
ohhai bash
$ cat /tmp/ohhai.py
#!/usr/bin/python2
print 'ohhai python'
$ perl /tmp/ohhai.py
ohhai python
$ cat /tmp/ohhai.groovy
#!/usr/bin/groovy
println 'ohhai groovy'
$ perl /tmp/ohhai.groovy
ohhai groovy
嗯...嗯?
为了确保我没有发疯,我尝试与其他解释器一起这样做并确认这只是一个 perl 主义:
$ python /tmp/ohhai.sh
File "/tmp/ohhai.sh", line 2
echo ohhai bash
^
SyntaxError: invalid syntax
$ ruby /tmp/ohhai.sh
ruby: no Ruby script found in input (LoadError)
$ bash /tmp/ohhai.py
/tmp/ohhai.py: line 2: print: command not found
这在某处有记录吗? thing/old 是新东西吗? ... 为什么?
"Swiss-Army chainsaw"确实如此。
这是perldoc perlrun
中记载的旧东西:
If the #!
line does not contain the word "perl" nor the word "indir" the program named after the #!
is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do #!
, because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.
当我不小心 运行 一个使用 perl 的 bash 脚本并且它......有效时,我的想法被震撼了。进一步试验,似乎 perl 读取脚本的 shebang 并发送给正确的解释器:
$ cat /tmp/ohhai.sh
#!/bin/bash
echo ohhai bash
$ perl /tmp/ohhai.sh
ohhai bash
$ cat /tmp/ohhai.py
#!/usr/bin/python2
print 'ohhai python'
$ perl /tmp/ohhai.py
ohhai python
$ cat /tmp/ohhai.groovy
#!/usr/bin/groovy
println 'ohhai groovy'
$ perl /tmp/ohhai.groovy
ohhai groovy
嗯...嗯?
为了确保我没有发疯,我尝试与其他解释器一起这样做并确认这只是一个 perl 主义:
$ python /tmp/ohhai.sh
File "/tmp/ohhai.sh", line 2
echo ohhai bash
^
SyntaxError: invalid syntax
$ ruby /tmp/ohhai.sh
ruby: no Ruby script found in input (LoadError)
$ bash /tmp/ohhai.py
/tmp/ohhai.py: line 2: print: command not found
这在某处有记录吗? thing/old 是新东西吗? ... 为什么?
"Swiss-Army chainsaw"确实如此。
这是perldoc perlrun
中记载的旧东西:
If the
#!
line does not contain the word "perl" nor the word "indir" the program named after the#!
is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do#!
, because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.