Philip Newton (pne) wrote,
Philip Newton

Porting from Perl to Python to Java

Quite a while ago, I started to post my weight to a community I created for that purpose, milixograke—in Klingon cheb and cheb'a', just for the heck of it.

A while later, I wrote a Perl script that would do the conversion from kg to cheb automatically, and post to LiveJournal (through the "flat" interface) while it was at it. I later added some code to convert to stone and pounds as well as calculate the BMI, then add that information to a database. (Haven't done anything with the data yet, though.)

That went all fine and dandy for over a year. A week or two ago, I thought I'd try to write the script in Python, mostly for practice in that language, since I had never really written anything in it.

That went reasonably well, after I figured out how to read and write HTTP and how to write to a database. What made the conversion slightly annoying is that Python is more strictly typed than Perl as regards strings vs. integers; in Perl, I could do something like $kg = sprintf("%.1f", $input); $lb = $kg / LB; (that is, use the string as a floating-point number), while in Python, I had to convert around with float() and int() a little bit. (Posts from the Python script go to milixobunda, for the terminally curious.)

Then yesterday, I thought I'd convert that to Java. My goodness, was that ever a lot of faffing around! Everything seemed more complicated and to involve more typing and looking things up than in Perl or Python.

Even after having got used to the need to convert a bit, doing so in Java seemed more complicated. For starters, the internationalisation got in the way; I wanted to be able to do something like sprintf("%.1f") for the subject line of the posts, but all the decimal-formatting things I found easily seemed to be locale-aware, so on my German Windows would give, say, "84,1 kg" which was not what I wanted. What I wanted was something equivalent to the "C" locale under C. Sites in the Web which discussed formatting numbers always seemed to use the default locale—which is pretty similar to the "C" locale when you're in the States, but not necessarily elsewhere. So that involved some faffing around with the DecimalFormatSymbols class, for example.

In the end, I got it to work… but it was fairly annoying along the way. Something might possibly have easier solutions that I just don't know about, but for now, it seemed that I was more productive in Perl and Python.

(Hm, I wonder whether I should give it a try in Tcl and Ruby, as well? Anyone have any other recommendations? Though completely different paradigms such as those of Haskell, OCaml, Smalltalk, or Lisp might be more challenging than I'm up for at the moment. Suggestions please only if accompanied by pointers to how to do (a) HTTP POST, (b) MD5, and, preferably, (c) how to insert a record into a MySQL database.)

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded