开发者

Porting Perl to Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

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 woul开发者_运维技巧d be helpful.


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


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.


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/JSON/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.


Unfortunately I ran in to the same pickle, there seems to be no direct way to do it. But if you are a vim savvy like I am you could quickly come up with a list like this one.

Just type all this in a file called(or something else) 'substitute' and open the Perl script(back up your original script first) and do :source path/to/the/substitute. Please note this is by no means perfect but might give you a good direction of start (Hopefully!)


    1,$s/\$\(.*\) /\1 /g
    1,$s/\$\(.*\)->/\1\./g
    1,$s/->/\./g
    1,$s/\$//g
    1,$s/ eq / \=\= //g
    1,$s/@//g
    1,$s/=>/:/g
    1,$s/use /import /g
    1,$s/::/\./g
    1,$s/||/or/g
    1,$s/&&/and/g
    1,$s/my //g
    1,$s/sub /def /g
    1,$s/ undef/ None/g
    1,$s/%//g
    1,$s/;$//g
    1,$s/\/os.environ/g
    1,$s/defined //g


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜