gpt4 book ai didi

Perl 基于 shebang 线派发给其他解释器?

转载 作者:行者123 更新时间:2023-12-04 11:03:42 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why don't I get any syntax errors when I execute my Python script with Perl?

(1 个回答)


6年前关闭。




当我不小心使用 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

这是在某处记录的吗?这是新事物/旧事物吗? ... 为什么?

确实是“瑞士陆军电锯”。

最佳答案

这是 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 基于 shebang 线派发给其他解释器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com