Perl to Python conversion

J Kenneth King james at agentultra.com
Thu Dec 10 14:04:52 EST 2009


martin.schoon at gmail.com (Martin Schöön) writes:

> First off: I am new here and this is my first post after
> lurking for quite some time.

Hi.

> Second off: I don't know much Python---yet.

It's not a very big language. If you have experience programming in
other languages, you can probably pick it up in a day or two.

> Problem: I have come across a small open source application
> that I find quite useful. It does have one major flaw though.
> Its output is in imperial units. Converting isn't a big deal
> for occasional use but if I start to use this stuff on a
> regular basis...
>
> So I down-loaded the source code and found this thing is written
> in Perl.
>
> Should I learn enough Perl to add the conversion? Probably
> but this may be a nice excuse to get my Python education
> going and if I do I might as well re-do the user interface.

Well you can always call it from Python via subprocess (which basically
wraps a shell and has fancy ways putting data in and extracting data
out).

> If I do re-write this thing in Python I might need to learn both
> Perl and Python...

You'll need to know one of them rather well and enough of the other to
get by.  It's probably easier to know more Perl than Python since Perl
is a lot more expressive than Python (in the TMTOWTDI sense).  Frankly I
learned Perl before Python and find it rather easy to go between the
two.  YMMV.

> Hence, are there any Perl to Python converters? So far I
> have only found bridgekeeper which really is (was?) consultancy.
> Apart from that I only find people recommending a manual re-write.

It depends where the two languages vary from one another.

If the script your translating uses basic types or even simple classes
and typical control structures and operations then translating from one
to the other is a simple matter of copy-pasting the code and translating
the syntax and small bits of grammar.

However, in areas where there are high variations; you'll probably want
to stay away from it.  Perl has a lot of freedom to manipulate
references and the programmer can modify the language to suit their
needs.  So just be careful of code that uses these features as they are
difficult to translate into Python.

> Any thoughts/recommendations?

Depends:

- If you needed it done yesterday to get some work done, wrap the Perl
  script in a subprocess and buy yourself some time to think it over.  
- If your purpose is to learn Python, then start from scratch.  Use the
  Perl as a guide if there are any maths or algorithms you are unsure
  about.
- If you're just hacking around to learn stuff, learn a little of both.
  It will make you smarter if it doesn't confuse the heck out of you and
  make you quit before you finish. ;)

>
> TIA,
>
> /Martin

HTH



More information about the Python-list mailing list