Programming Perl Instead of Ruby

Posted on July 12, 2008

I've got a new job and it's working almost completely in Perl. Who'd have thought? Fortunately, it really is true that if you know one programming language fairly well, you'll understand others fairly easily. Modifying code in Perl is easy as long as I have other code to refer to, and I often forget it's a new language. Learning all the names, faces and practices in my new job is much harder than learning a bit of new syntax. However, if I had to sit down and write something in Perl from scratch it would not go well.

There's definitely a few things I've noticed about Perl that I don't like compared to Ruby:

  • Sigils. I keep forgetting to prepend my variables with those $@% sigils. That's not me cursing there, those are the sigils for scalars, arrays and hashes. The worst is when you're references the whole array (@foo) you use a different sigil than when you're referencing a scalar element in the array ($foo[0]).
  • Objects feel like a hacked up addon to the language. You have to "bless" things to make them objects and you're always passing around references to $self to object methods. You can't make methods private. Weird stuff.
  • No interactive shell built in. I loved having irb to try things out when I was learning Ruby - heck, I still do. Fortunately, after a bit of searching and bad advice on just using the Perl debugger I found Perl Console.
  • Always needing to say "my variablename" instead of just "variablename" if I want local variables. It seems like variables should be local by default and you say something if you want to make them local.

There's some things about Perl I'm not sure if I like or dislike yet:

  • Default variables. Strangely named variables like $_ and @_ pop up everywhere. The idea is if you don't want to come up with a name for variables while you're iterating or passing parameters, you don't have to thus saving all that typing and thinking. However it certainly makes the code harder to read for those who don't know Perl and I suspect it might make things always harder to read even once I get used to it.
  • Regular expressions everywhere. This will be good for me to get better with this powerful feature, but damned if regular expressions aren't ugly.
  • There's always more than one way to do something. This is true in most every language, but Perl seems to take a lot of pride in it, and often the idiomatic Perl way of doing something is quite a bit harder to read than the non idiomatic way.

The main thing I like about Perl so far is that the Perl community seems to have a sense of humor. Perl books are always making jokes and variables in Perl code are given entertaining names.

Aside from the new language difference, the new job is great for me to get used to coding in a large codebase and with teams. We use quite a few Extreme Programming practices like test driven development, and even more impressive, pair coding. These practices more than make up for anything I don't like about Perl.

Units In Ruby

Posted on February 13, 2007

I'm currently working on a sort of cookbook rails application for my wife, who is in culinary school, and just for fun. I've just started, but I'm feeling fairly ambitious with plans for searchable and rateable recipes, automatic food costing, meal planning and maybe nutrition information. Heck, maybe I'll even open source it or sell it as a subscription service.

However, I've barely begun and quickly run into a problem: how to deal with all those crazy imperial units! Pounds or lbs, ounces or ozs, floz, cups, quarts and everything else with weird abbreviations and conversion systems that suck. Of course, like most problems, somebody else out there already has a great solution. Kevin Olbrich at SciWerks.com has created Ruby-Units.

gem install ruby-units

At first I wasn't sure I liked the way it used strings to create Units, but after a bit of use I really started to dig it, especially when I saw you could put in mishmash units like happens so often in cooking.

"4lbs 6oz".unit # => 4.375 lbs
"4pounds 6oz".unit # => 4.375 lbs

Also very cool and helpful to me was the ability to check to see if units were compatible with each other.

"2 cup".unit.compatible? "qt".unit # => true
"2 cup".unit.compatible? "lb".unit # => false

One of my only complaints/problems with this so far is that I don't see a built in method to return all compatible units. If there was one, I imagine that it would work like this:

"2 cup".unit.compatible_units # => ["tbsp", "tsp", "pint", "qt", "gal"] etc

Also I may need to at some point build in my own conversions for specific food types. For example, 1 gallon of water = 8.33 pounds. I'm not sure how easy these sort of conversions will be to implement.

There are a few other unit libraries for ruby that I found. There's a package simply called Units that doesn't look like it's active anymore, but it did have some documentation right up front about adding conversion types. There's also the Ruby Facets libraries, but I haven't looked through them much yet. They only mention SI units, so I don't know if they support Imperial units.