87

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.

9
  • 6
    I'm guessing it's because of the #! in the beginning of the file. Indeed if I remove the she-bang, I'm getting the expected behavior. Isn't that a bad idea from the security perspective, anyway? Commented Apr 10, 2015 at 14:20
  • 8
    No. The point of the shebang path it to specify an interpreter. If you don't trust the code to run, then you shouldn't be running it in the first place. Commented Apr 10, 2015 at 14:21
  • 1
    No, not really. Your script is a text file. No more, no less. It won't 'run' without an interpreter. Commented Apr 10, 2015 at 14:23
  • 4
    "Why is my Perl interpreting my Python?" is not "a problem that can no longer be reproduced or a simple typographical error." Voted to reopen. The upvotes on the Q and the A show this is a question of popular interest. Commented Apr 10, 2015 at 20:28
  • 2
    @ikegami Regardless of popularity, this is clearly not "a simple typographical error...resolved in a manner unlikely to help future readers." Voted to reopen. Commented Apr 10, 2015 at 20:37

1 Answer 1

114

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
Sign up to request clarification or add additional context in comments.

8 Comments

Wow. Talk about your obscure features. I've been using Perl for more than 20 years, and I had no idea it did that.
I started using Perl v4 on DOS, VMS & Solaris. It's OS agnostic/bridging features like this that made cross platform life so much easier.
@MarcvanLeeuwen When you write programs for Linux, OSX, VAX/VMS, Windows, Solaris, OS/2, and whatever else the most annoying part of porting a script-language program is getting it started, as many of these systems, though usually sharing the "type a command, have it found & executed" feature in common, do nearly everything else differently. This Perl feature makes Perl an easy gap-bridge in functionality -- you can write just a Unix-style shebang and if Perl is present the code, whether Perl code or not, will always work -- its like a more universal #! /usr/bin/env foo.
This just cements Perl's reputation as the kitchen sink of programming languages. As an interesting observation, I believe the original implementation of shebang was as a shell feature, it only moved into the Unix kernel later. Perl includes many shell mechanism (e.g. backticks for substituting command output), this is just one more.
ruby does the same >.<
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.