If you visit this site in a browser instead of reading the RSS feed, as very few of you do, you’ve probably noticed the out-of-hand blogroll over there in the sidebar. I build that using a Perl script that periodically grabs my Bloglines OPML file and converts it to a static HTML file that gets included when this page is generated. For a long time, I was doing some really fragile XML parsing in the conversion. When I started using subcategories and the OPML changed, the parsing broke and the blogroll was incomplete. I knew it was coming, but rats!

I’d intended to use XPath for a long time, but never quite got around to setting it up. It’s not as if XML::XPath is a difficult module to use — Matt Sergeant knows his stuff and has done wonders for using XML in Perl — but somehow other things took priority over figuring out how to go about this.

Then I started learning Ruby. The XML library that ships as part of the Ruby Standard Library looked easy to use, so my very first attempt at writing something in Ruby tackled the problem I’d been avoiding:

require 'rexml/document'
require 'rexml/text'
require 'uri'
include REXML

xml = Document.new(File.open("blogroll.opml"))
links = {}
xml.elements.each("//outline") do |o|
 htmlUrl = o.attributes['htmlUrl']
 unless (htmlUrl.nil? || htmlUrl.empty? )
  links[ o.attributes['title'] ] = htmlUrl
 end
end

links.sort.each do |title,url|
 puts "<li>
  <a href=\"#{ Text.normalize( URI.escape(url) ) }\">
  #{ Text.normalize(title) }
  </a></li>"
end

I’m sure it could be improved in any number of ways, but it worked! And writing it just felt natural. Unfortunately, my web host has an older version of Ruby without REXML. I could probably route around that, but now that I’d written the code I knew the way forward with Perl. A few minutes later, I’d updated the script and was in business.

This is why I like the idea of learning at least one new language a year: I’m exposed to new ways of thinking about a problem, or am inspired to work on something I might otherwise avoid.

But something else is happening, too. I use several languages, but have long felt most at home in Perl. That’s beginning to change. Writing Ruby code feels right, it fits how I want to write code. Maybe some of that’s just the heady rush of a love affair. Certainly some of it is an extension of a approach to programming that I first discovered in Perl and that makes both Ruby and JavaScript fun to work with. (More on that later.) It’s not important, really: I’m having fun. That’s enough.