gpt4 book ai didi

python - 为什么我在使用 Perl 执行 Python 脚本时没有收到任何语法错误?

转载 作者:IT老高 更新时间:2023-10-28 21:11:37 28 4
gpt4 key购买 nike

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

perl test.py

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

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

#!/usr/bin/python

...Python code here...

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

最佳答案

来自 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.

例如,

$ cat a
#!/bin/cat
meow

$ perl a
#!/bin/cat
meow

关于python - 为什么我在使用 Perl 执行 Python 脚本时没有收到任何语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563832/

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