in reply to did you mean python

... as the code is run through the python3 interpreter, you will need to write a polyglot program that runs under Python as well as under Perl. Under Python, the program should compile (as not to raise a meaningless syntax error) but then output an error message, while under Perl, the program should run as-is.

Ideally the Python implementation would restart the program under Perl.

I can easily see a Python module import working, but I'm unsure about the compile-time / run-time semantics and implementation of Python, and whether the top unit must compile completely before imported code is run. The following code could work under Perl and Python:

import RunPerlCodeFromPython

This would be interpreted as valid Python code, importing the RunPerlCodeFromPython module.

Under Perl, this would be interpreted as RunPerlCodeFromPython->import().

The problem is that Perl and Python syntax are largely incompatible and you need to find a good escape hatch to make the Python interpreter ignore all lines following this import. Maybe a cleverly crafted here-document can help there.

Replies are listed 'Best First'.
Re^2: did you mean python
by harangzsolt33 (Deacon) on Jan 27, 2026 at 17:59 UTC
    you will need to write a polyglot program that runs under Python as well as under Perl

    I would love to see something like that!!! That would be interesting! But how would one even begin something like that? I mean the first line in a perl program "#!/usr/bin/perl" prevents it from being interpreted as a python program. No?

      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". ;-)