tags:

views:

272

answers:

6

I am wondering if there are any tips or tricks to converting perl into python. It would be nice if there was a script like python's 2to3. Or perhaps some compatibility libraries. It doesn't have to be complete, anything to help speed up the process would be helpful.

+8  A: 

the best "tool" to convert Perl to Python is still yourself :). Learn the syntax of Python and how it works from Python docs. Then do the conversion manually. for example, Perl makes use of regex a lot, however, in Python, string manipulation is so easy that most of the time, regex is not needed. How would a converter know this beforehand?? There are also many ways to do things in Perl, so how would a converter know what is the best with Python equivalents ? therefore, manual conversion is still the best way to go

If you are interested, you can take a look at this book Also of interest, Pleac

ghostdog74
"Perl makes use of regex a lot, however, in Python, string manipulation is so easy that most of the time, regex is not needed"? Seriously?
AmbroseChapel
The only reason why string manipulation in python seems easy is because it's harder to do the more complicated string manipulations, so people generally don't.
Michael
@Ambrose, i can't really answer you unless you have extensive knowledge of Python and Perl, and can see for yourself the difference.
ghostdog74
I don't know Python well enough to comment on its differences from Perl, buuuuuut... As a general rule, "I can't explain the answer unless you already know enough that I won't have to explain it" is a cop-out answer which reeks of "I don't really know what I'm talking about." Any topic you can't at least explain to a layman in broad terms is a topic that you don't fully understand yourself.
Dave Sherohman
@dave, it is not that i don't want to explain. There are many cases. If you are interested, you are welcome to take a look at http://pleac.sourceforge.net/pleac_python/strings.html and http://pleac.sourceforge.net/pleac_perl/strings.html for examples on how string manipulation is easy in Python, (especially doing substringing). Finally, i have been using Perl/Python for many years, so yes, i do know what i am talking about.
ghostdog74
Looking at the PLEAC stuff, what we have here is a case of a rote translation of a technique from one language causing another to look bad. For example, its rare in Perl to work character-by-character. Why? For one, its a pain in the ass. A fair cop. For another, you can usually do it faster and easier with a regex. One can reverse the OP's statement and say "in Perl, regexes are so easy that most of the time other string manipulation is not needed". Anyhow, the OP's sentiment is correct. You do things differently in Perl than in Python so a rote translator would produce nasty code.
Schwern
Of course, the really silly thing about the original comment is that in Perl, we're doing "regex", but in Python, they're doing "string manipulation" ... ever heard of a Venn diagram? Those two things are very often the same thing.
AmbroseChapel
+5  A: 

There are so many ways of doing the same thing in Perl that writing any sort of crosscompiler would be... painful at best. Reconstruct (don't just translate) the program manually.

Ignacio Vazquez-Abrams
+2  A: 

Converting would require writing a Perl parser, semantic checker, and Python code generator.

Not practical. Perl parsers are hard enough for the Perl teams to get right. You'd be better off translating Perl to Python from the Perl AST (opcodes) using the Perl Opcode or related modules.

http://perldoc.perl.org/Opcode.html

Some notations do not map from Perl to Python without some work. Perl's closures are different, for example. So is its regex support.

In short, either convert it by hand, or use some integration modules to call Python from Perl or vice-versa.

mrjoltcola
+5  A: 

Keep a phrasebook handy as you go through the code you want to port.

mobrule
Thank you for actually answering my question instead of telling me why I shouldn't/can't do it.
Rook
+2  A: 

Why convert? >;-)

andrew -- Perl lover...

drewk
I ditched perl after I couldn't stop my mutli-threaded perl script from crashing. (+1 don't give up.)
Rook
because Python syntax is easier on the eye, plus Python code is more maintainable for large projects, etc
@The Rook: FWIW, the threading library for Perl is called "Coro", not "threads". Also, why bother with multi-CPU threads in a language like Perl or Python? Using 2 cores will give you a 2x speedup. Switching to Haskell will give you a 50x speedup.
jrockway
@jrockway, Yeah I could also stab myself in the face with a fork.
Rook
@The Rook: Unrelated, as that would not improve the runtime performance of your code.
jrockway
@jrockway LOL, wow I can't disagree with that statement. But for this project i'm porting some perl libraries to a python project(w3af).
Rook
+2  A: 

Another approach is that you might be able to reuse existing Perl code and complement it with new Python code.

One possibility that might work for you is to use the perl Python module, which allows you to execute Perl code inside Python.

Another approach is to have Perl produce its output as simple text/YAML files which is parsed by Python for further processing. Thus, I was able to use my legacy Perl code without rewriting it, while switching to Python. Your mileage may vary, as this "gluing" method might be too slow for your needs. This "pass files" method is good when you have long calculations that produce intermediate results: you can start the computation in Perl, and continue it in Python.

EOL