Closures and the professional programmer

Posted by Tom Moertel Tue, 30 Aug 2005 18:56:00 GMT

I came across Tim Bray’s thoughts on Ruby via the ever-delightful Lambda the Ultimate and found the following bit fascinating:

I’ve had access to languages with closures and continuations and suchlike constructs for years and years, and I’ve never ever written one. While I’m impressed by how natural this stuff is in Ruby, I’m still unconvinced that these are a necessary part of the professional programmer’s arsenal. [Emphasis mine.]

While Tim Bray may be unconvinced, I am a true believer.

Read more...

Posted in , , ,
Tags , ,
12 comments
1 trackback
Reddit Delicious

Power parsing with Haskell and Parsec

Posted by Tom Moertel Sat, 27 Aug 2005 21:12:00 GMT

One of the projects I’m working on is a language to help researchers manipulate genetics information. Despite all the well-publicized advances in genetics, researchers still spend about a third of their time writing shell, awk, and Perl scripts to manipulate their data. If researchers can get some of this time back, they can use it to think about more interesting problems, like curing cancer and stuff like that.

Read more...

Posted in ,
Tags , ,
2 comments
no trackbacks
Reddit Delicious

I have moved my blog over to Typo

Posted by Tom Moertel Thu, 25 Aug 2005 20:41:00 GMT

SnipSnap no longer makes me happy, and I am switching my weblog over to the Typo weblog system and moving it to blog.moertel.com. I like Rails, and Typo is Rails-based coding at its finest. And Typo has a future. Besides, I want another opportunity to take a perfectly good website theme and destroy it with my utter lack of design acumen.

As far as content goes, I will move my old posts over as time permits. For now, you can read them on the old site, which will remain up. (The Community Projects are still hosted there.)

If you are subscribed to my old feeds, don’t worry: Apache trickery will automagically redirect your RSS reader to my new feeds here. If fact, if you are reading this, you are already getting the new stuff.

—Tom

Posted in ,
6 comments
no trackbacks
Reddit Delicious

How to change symlinks atomically

Posted by Tom Moertel Mon, 22 Aug 2005 16:00:00 GMT

Many people don’t realize that changing the target of a symbolic link (symlink) is not an atomic operation. “Changing” a symlink really means deleting it and creating a new link with the same file name. For example, if I have a symlink current that points to a directory old, and I want to change it to point to a directory new, I might use the following command:

$ ln -snf new current

Strace shows what really happens when I run the command:

$ strace ln -snf new current 2>&1 | grep link
unlink("current")         = 0
symlink("new", "current") = 0

First, the existing symlink is deleted via the unlink system call. Then a new, identically named symlink is created via the symlink system call. It’s a two-step process, and in between the steps, there is no symlink.

This can be a problem if you expect the symlink to be there always, such as when using the link to point to the active version of a live web site. If you change the symlink while deploying a new version of your site, for example, the web server might try to dereference the link during the small window of time when it doesn’t exist. Oops.

The solution to this problem is to effect the change by creating a new symlink and then renaming it over the old symlink. On Unix-like systems, renaming is an atomic operation, and thus the symlink “change” will be atomic too. By hand, the process looks like this:

$ ln -s new current_tmp && mv -Tf current_tmp current

In Ruby, I make atomic symlinking available everywhere by extending the Pathname class with a new method atomic_symlink:

require 'pathname'

class Pathname
  def atomic_symlink(old)
    suffix = [Array.new(6){rand(256).chr}.join].pack("m").strip.tr('/','_');
    tmplink = Pathname.new(self.to_s + "_" + suffix)
    tmplink.make_symlink(old)
    begin
      tmplink.rename(self)
    rescue
      # if rename fails, we must remove the temporary link manually
      File.unlink(tmplink.to_s)
      raise
    end
  end
end

This code is nothing more than a robustified version of the by-hand method. It picks better names for temporary links, and it cleans up after itself, should something go wrong, but otherwise it does the same thing.

Given how easy it is to change symlinks atomically, why do it any other way? Life is hard enough without having to worry about another race condition.

Posted in , ,
Tags , ,
5 comments
no trackbacks
Reddit Delicious

Perl Mongers meet at PAPA-8 pinball tournament kickoff!

Posted by Tom Moertel Fri, 12 Aug 2005 16:00:00 GMT

Tonight’s meeting of the Pittsburgh Perl Mongers was held at the World Headquarters of the Professional Amateur Pinball Association to coincide with the PAPA 8 World Pinball Championships.

To say it was a cool meeting doesn’t do it justice. Not only did we hear a fun talk on writing CGI-contained Mason applications by our own Dan Wright, but after the meeting we wandered around and played fabulous, well-maintained vintage pinball machines and fine 1980s-era video games. And we watched the world’s best pinball players compete. And we ate junk food. Yeah!

By all means, do look at the PAPA 8 photos. And if you need the love that only good pinball can provide, experience PAPA 8 for yourself: the tournament runs for the next few days.

A big thanks to PAPA for hosting us!

—Tom

Posted in , ,
no comments
no trackbacks
Reddit Delicious