Why is it impossible to create a practical Perl converter for Python source code?

It would be nice if there was a program that automatically converts Perl code to Python code, making the resulting Python program as readable and supported as the original, not to mention working the same way.

The most obvious solution will simply call perl through Python utils:

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

However, the resulting code is hardly Python code; it is essentially Perl code. A potential converter should convert Perl constructs and idioms into easily readable Python code, it should save the names of variables and subprograms (i.e. the result should not look confusing) and should not destroy wrokflow too much.

Such a conversion is obviously very complicated. The hardness of the conversion depends on the number of Perl functions and syntax constructs that do not have easily readable, not distorted Python equivalents. I believe that a large number of such functions make such an automatic conversion practically impossible (while a theoretical possibility exists).

So, could you name the names and features of the Perl language that cannot be expressed in Python as compressed as in the Perl source code?

Edit : Some people bind the Python-Perl conventers, and on this basis deduced that it should also be easy to write Perl-to-Python. However, I'm sure the conversion to Python is in high demand; this converter has not yet been written - while the opposite has already happened! This only confirms my belief in the impossibility of writing a good Python converter more robust.

+4
source share
11 answers

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

  • dynamic region (using the local keyword)
  • glob type manipulation (several variables with the same name)
  • (they have all the syntax)
  • closes variables variables
  • pseudo-comments
  • lvalue routines ( mysub() = 5; type code)
  • source filters
  • context (list vs scalar, and the way the called code can check this with wantarray )
  • Force Type / Dynamic Typing
  • any program that uses the string eval

The list is included, and someone might try to create a mapping between all similar constructs, but in the end it will fail for one simple reason.

Perl cannot be statically analyzed. The definitions in Perl code (especially in BEGIN blocks) change the way the compiler interprets the rest of the code. Therefore, for non-trivial programs, the conversion from Perl => Python suffers from a stopping problem.

It is not possible to know exactly how the entire program will be compiled until the program ends, and it is theoretically possible to create a Perl program that will compile differently each time it is launched. This means that one Perl program can display an infinite number of Python programs, the correct of which is known only after starting the original program in the perl interpreter.

+17
source

Your best Perl to Python converter, probably 23 years old, has just graduated from university and is looking for work.

+34
source

Why Perl is not Python.

  • Perl has instructions that Python is more or less missing. Although you can probably sort out matching statements, the syntax will be so completely different from Perl that it makes it difficult to call it "translation". You really need to cook some Python fancy stuff to make it as concise as the original Perl.

  • Perl has run-time semantics that are so unlike Python that makes translation very difficult. Below we consider only one example.

  • Perl has data structures that are quite different from Python, which makes translation difficult.

  • Perl threads do not share default data. Only selected data items are available. Python threads have more common "shared" data.

One example from # 2 should be sufficient.

Perl:

 do_something || die() 

Where do_something is any instruction of any kind.

To automatically convert this to Python, you will need to wrap each expression || die() || die() in

 try: python_version_of_do_something except OrdinaryStatementFailure, e: die() sys.exit() 

Where is the more common wording

Perl

 do_something 

Would it with a simple - mindless - translation of the source

 try: python_version_of_do_something except OrdinaryStatementFailure, e: pass 

And of course,

Perl

 do_this || do_that || die() 

Even harder to translate to Python.

and

Perl

 do_this && do_that || die() 

really click on the envelope. My Perl is rusty, so I can’t remember the exact semantics of this kind of thing. But you must fully understand the semantics for developing a Pythonic implementation.

Python examples are not good Python. To write good , Python requires “thinking,” something automatic translation cannot do.

And each Perl construct must be “wrapped” in such a way as to get the original Perl semantics in the form of Pythonic.

Now do a similar analysis for each Perl function.

+25
source

This is not impossible, it just takes a lot of work.

By the way, there is Perthon , a Python-to-Perl translator. It just seems that no one wants to do what goes the other way.

EDIT: I think I could find a reason why translating Python to Perl is much easier to implement. This is because Python allows you to play with the AST script. See analyzer module.

+7
source

Perl can be experimentally built to collect additional information (for example, comments) during compilation of Perl code and even emit the results as XML. It seems that there is no documentation about this outside of the source, except for: http://search.cpan.org/perldoc/perl5100delta#MAD

This should help in creating a translator. I would expect you to get 80% of the way there quite easily, 95% with great difficulty, and never much better. Too many things that do not display well.

+5
source

In essence, these are two different languages. Converting from one to another and the result is mainly for reading means that the software would have to recognize and generate code idioms and be able to perform some static analysis.

The value of the program can be precisely determined by the definition of the language, but the programmer does not necessarily require all the details. C-programmer tester, if the return value of a printf() negative, checks the error condition and usually does not care about the exact value. if (printf("%s","...") < 0) exit(); can be translated to Perl as print "..." or die(); . These statements may not mean the same thing, but usually it will be what the programmer means, and create idiomatic C or Perl code from idiomatic Perl or C code that the translator should consider.

Since different computer languages ​​tend to have different little semantics for such things, it is usually not possible to translate one language into another and come up with the same meaning in a readable form. To create readable code, the translator must understand what the programmer intended to do, and this is very difficult.

Also, it would be easier to switch from Python to Perl, rather than Perl to Python. Python is designed to be a simple language with clear, standard ways, and Perl is too complex a language with the motto "There's more than one way to do this." Translating a Python expression into one of the countless matching Perl expressions is easier than figuring out what the Perl programmer means and expressing it in Python.

+5
source
  • The scope and space of Python is different from Perl.

  • In Python, everything is an object. In Perl, everything under the hood looks like a list / hash / scalar / link / function. This causes a variety of design approaches and idioms.

  • Perl has anonymous blocks of code and can generate locks on the fly with some branches. I am sure this is not a python function.

I think a very smart person could statically analyze most of Perl and create a program that accepts small Perl programs and outputs Python programs that do the same job.

I much more doubt the possibility of a large and / or gnarly translation of Perl. Some of us sometimes write really funky code .... :)

+3
source

This is not possible just because you cannot even parse Perl code correctly. See Perl cannot be analyzed: formal proof for more details.

+2
source

B Malcolm Beattie B module set will be the only starting point for something similar, although I have other answers that it will be a difficult problem to solve. In general, translating the meaning of one high-level language to another high-level language requires a high-level translator, and so far this can only mean a person.

The complexity of this problem for any pair of languages ​​is due to fundamental differences in the nature of the languages ​​in question, such as runtime semantics and general idioms, not to mention libraries.

+1
source

The reason it is not possible to create a common translator from one high-level language to another is because the program describes only HOW, not WHY (this is the reason for comments in the source code).

To create a meaningful program in another high-level language, you (or a translator program) must know WHY in order to be able to create the best possible program. If you cannot do this, all you can do is essentially create a Python interpreter for the compiled version of the Perl program.

In other words, to do this correctly, you need to go beyond the window, and it is very difficult for a computer.

+1
source

A NullUserException basically summed it up - it certainly can ; it would be just a huge amount of effort for this. Some language conversion utilities that I saw are compiled into an intermediate language (such as .NET CIL), and then decompiled into the desired language. I have not seen for Perl for Python. You can, however, find Python for the Perl converter here , although this is unlikely for you if you are not trying to create your own, in which case it can provide some useful link.

Edit: if you only need the exact functionality in a Python script, PyPerl may come in handy.

0
source

All Articles