Search code examples
pythonperltranspiler

Why is it not possible to create a practical Perl to Python source code converter?


It would be nice if there existed a program that automatically transforms Perl code to Python code, making the resultant Python program as readable and maintainable as the original one, let alone working the same way.

The most obvious solution would just invoke perl via Python utils:

#!/usr/bin/python
os.exec("tail -n -2 "+__file__+" | perl -")
...the rest of file is the original perl program...

However, the resultant code is hardly a Python code, it's essentially a Perl code. The potential converter should convert Perl constructs and idioms to easy-to-read Python code, it should retain variable and subroutine names (i.e. the result should not look obfuscated) and should not shatter the wrokflow too much.

Such a conversion is obviously very hard. The hardness of the conversion depends on the number of Perl features and syntactical constructs, which do not have easy-to-read, unobfuscated Python equivalents. I believe that the large amount of such features renders such automatic conversion impossible practically (while theoretical possibility exists).

So, could you please name Perl idioms and syntax features that can't be expressed in Python as concise as in the original Perl code?

Edit: some people linked Python-to-Perl conventers and deduced, on this basis, that it should be easy to write Perl-to-Python as well. However, I'm sure that converting to Python is in greater demand; still this converter is not yet written--while the reverse has already been! Which only makes my confidence in impossibility of writing a good converter to Python more solid.


Solution

  • Just to expand on some of the other lists here, these are a few Perl constructs that are probably very clumsy in python (if possible).

    • dynamic scope (via the local keyword)
    • typeglob manipulation (multiple variables with the same name)
    • formats (they have a syntax all their own)
    • closures over mutable variables
    • pragmas
    • lvalue subroutines (mysub() = 5; type code)
    • source filters
    • context (list vs scalar, and the way that called code can inspect this with wantarray)
    • type coercion / dynamic typing
    • any program that uses string eval

    The list goes on an on, and someone could try to create a mapping between all of the analogous constructs, but in the end it will be a failure for one simple reason.

    Perl can not be statically parsed. The definitions in Perl code (particularly those in BEGIN blocks) change the way the compiler is going to interpret the remaining code. So for non-trivial programs, conversion from Perl => Python suffers from the halting problem.

    There is no way to know exactly how all of the program will be compiled until the program has finished running, and it is theoretically possible to create a Perl program that will compile differently every time it is run. Meaning that one Perl program could map to an infinite number of Python programs, the correct of which is only know after running the original program in the perl interpreter.