为什么在使用 Perl 执行 Python 脚本时没有出现任何语法错误? [英] Why don't I get any syntax errors when I execute my Python script with Perl?
问题描述
我刚刚在 test.py
中编写了一些测试 python 代码,然后我按如下方式启动它:
I just wrote some testing python code into test.py
, and I'm launching it as follows:
perl test.py
过了一会儿,我意识到了我的错误.我说过一会儿",因为Python 代码实际正确执行,就像在 Python 解释器中一样!
After a while I realized my mistake. I say "after a while", because the Python code gets actually correctly executed, as if in Python interpreter!
为什么我的 Perl 解释了我的 Python?test.py
看起来像这样:
Why is my Perl interpreting my Python? test.py
looks like this:
#!/usr/bin/python
...Python code here...
有趣的是,如果我做相反的事情(即调用 python something.pl
),我会遇到很多语法错误.
Interestingly, if I do the opposite (i.e. call python something.pl
) I get a good deal of syntax errors.
推荐答案
来自 perlrun,
如果 #!
行既不包含单词perl"也不包含单词indir",则执行以 #!
命名的程序而不是 Perl 解释器.这有点奇怪,但它可以帮助那些不执行 #!
的机器上的人,因为他们可以告诉程序他们的 SHELL 是 /usr/bin/perl,然后 Perl 会将程序分派给正确的解释器.
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.
例如
$ cat a
#!/bin/cat
meow
$ perl a
#!/bin/cat
meow
这篇关于为什么在使用 Perl 执行 Python 脚本时没有出现任何语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!