Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

perl

I just wrote some testing python code into test.py, and I'm launching it as follows:

perl test.py

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!

Why is my Perl interpreting my Python? test.py looks like this:

#!/usr/bin/python

...Python code here...

Interestingly, if I do the opposite (i.e. call python something.pl) I get a good deal of syntax errors.

like image 498
Dacav Avatar asked Apr 10 '15 14:04

Dacav


People also ask

How do you call a Python script in Perl?

You can easily capture STDERR redirecting it to STDOUT: my $ret = `/usr/bin/pdf2txt.py arg1 arg2 2>&1`; If you need to capture the exit status, then you can use: my $ret = system("/usr/bin/pdf2txt.py arg1 arg2");

What is a syntax error in Python?

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program. Example: Omitting the colon at the end of a def statement yields the somewhat redundant message SyntaxError: invalid syntax.


1 Answers

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
like image 139
ikegami Avatar answered Oct 12 '22 17:10

ikegami