I mean the first line in a perl program "#!/usr/bin/perl" prevents it from being interpreted as a python program.

See perlrun: The shebang line does not have to be the first line in the file. You can make the perl interpreter skip a lot of lines in the file until it finds a line starting with "#!" and containing "perl".

And the shebang line is not required at all. You can start your perl script right in the first line of a file, without a shebang line. Many operating systems don't use the shebang line at all, in fact, it is specific to the Unix family of operating systems. Other systems use file name extensions, file name prefixes, or some other mechanisms. Most other scripting languages, including Python, also do not need the shebang line.

So, you can get a very simple polyglot program just like this:

print("Hello World")

Not very impressive, but it is both a valid Perl script and a valid Python script. The syntax is similar enough for this to work.

Of course, things get more complicated if you want more than just "Hello World". That's where you need to be creative. Find a way for the two (three, four) languages to make them interpret the same text in a different way. Like this:

@rem =(' @echo off echo Hello World from a plain old DOS batch file goto end '); print "Hello World from perl\n"; __END__ :end

This is both a DOS batch file and a Perl script. DOS batches don't print lines starting with @ and ignore everything following rem. They also do not interpret lines that aren't executed. So for DOS, this is a non-printed comment (@rem), a non-printed command to disable printing (@echo off), a print statement (echo), a jump to a label at the end of the file, three lines that are not executed, and a jump label (:end). For Perl, this is an assignment of a multi-line string to the array @rem, followed by a print, followed by an end-of-script marker, followed by data which is not processed by this script. The assignment to @rem just wastes a little bit of RAM and CPU, but does nothing except skipping the DOS batch script.

perlrun shows how a script can be both a (Unix) shell script and a perl script, where the former starts the latter. This trick is used to work around problems in ancient operating systems. A similar trick is used in Strawberry Perl, where batch files start embedded Perl scripts.

And another little detail may be useful on Unix: If a text file is made executable, is invoked as a program, and the kernel won't find a shebang line as line 1 or some other file identifier, the shell will execute the text file as a shell script:

$ echo echo Hi > hi $ chmod +x hi $ ./hi Hi $ cat hi echo Hi $

See also Re^2: Shebang behavior with perl.

<Update>

The perl interpreter has another little detail that might suprise you, but it is meant to be helpful:

$ cat foo.pl #!/bin/bash echo Hello World from Bash $ perl foo.pl Hello World from Bash $

If perl sees a shebang line for another interpreter, it happily replaces itself with the other interpreter. So even if this file is started via perl and has a .pl extension, it will be executed by /bin/bash.

</Update>

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re^3: did you mean python by afoken
in thread did you mean python by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.