使用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代码!
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屋!