为什么在使用 Perl 执行 Python 脚本时不会出现任何语法错误?

我刚刚在 test.py中编写了一些测试 python 代码,我将按如下方式启动它:

perl test.py

过了一会儿,我意识到了我的错误。我说“过了一会儿”,因为 Python 代码实际上得到了正确的执行,就像在 Python 解释器中一样!

为什么我的 Perl 解释我的 Python? test.py看起来像这样:

#!/usr/bin/python


...Python code here...

有趣的是,如果我做相反的事情(即调用 python something.pl) ,我会得到大量的语法错误。

3912 次浏览

From 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.

For example,

$ cat a
#!/bin/cat
meow


$ perl a
#!/bin/cat
meow