Perl根据shebang线派遣到其他口译员?

前端之家收集整理的这篇文章主要介绍了Perl根据shebang线派遣到其他口译员?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Why don’t I get any syntax errors when I execute my Python script with Perl?1个
当我不小心使用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-ism:

$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

这是在某处记录的吗?这是一件新事物吗? ……为什么?

确实是“瑞士军队电锯”.

解决方法

这是 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相关文章