Decorated JavaScript

I’ve just read The Decorator Pattern for JavaScript. It’s a beautiful example of precisely how flexible a language like JavaScript is. The technique described allows you to run functions before and/or after any function on any object.

The code is fairly understandable as well. When you set up an object as “Decorated”, it adds before() and after() methods. You pass in the name of the function you want run before and another function. Then the magic kicks in. It takes a copy of the original method, and installs a new method in its place which calls all the before hooks, then the original method and finally any after methods.

The only minor disadvantage is that you can’t inspect or alter the return value in the after method. Otherwise, this piece of code is conceptually very similar to Perl’s Hook::LexWrap (a damned fine tool).

I particularly like the way that you can apply the decorator to either all objects or a particular instance thanks to JavaScript’s lovely prototype based OO.

Yes, I’m sure that Dojo gives you all this too. But this is a nice isolated example.



Watchmen is the wikipedia featured article today! If you haven’t already read it, do yourself a favour and go and buy a copy.


Character Info in Textmate

One rather useful feature of vim is that you can pull up information about a character by positioning your cursor over it and hitting ga (get ASCII?). I quite miss this in textmate, so I created a small command to add to the Text bundle. This is “Character Info”, which I’ve assigned to ^⇧I. It takes the selection as input and the information comes back in a tooltip.

  use strict;
  use warnings;
  use charnames qw( :full );
  binmode( STDIN, ':utf8' );
  foreach my $c (split //, do { local $/; <> }) {
      my $code = ord $c;
      my $name = charnames::viacode( $code ) || "unknown character";
      printf "U+%04X %sn", $code, $name;
  exit 0;

This is what it looks like.

Character Info in action

The only caveat is that it only works if you’re using UTF-8 for your files. But really, if not, why not?


System Keychain

This morning I was trying to add a new machine to my wireless network. Unfortunately, I’d forgotten the password… To the Keychain Access batcave!

Unfortunately, the “Airport network password” is stored in the system keychain, instead of my login keychain. Whilst I can unlock the system keychain, when I ask it to show me the password for my wireless network, it prompts for a password. Not my password, as it happens. Oh no. System keychain is protected by a 48 random bytes stored in /var/db/SystemKey. It’s created by the systemkeychain utility the first time your mac is booted. More to the point, there’s absolutely no way I can type those bytes.

So, let’s be cunning I thought. I dropped down to the command line and ran:

  % sudo cat /var/db/SystekMey | pbcopy

Then went back to keychain access only to discover that you can’t paste passwords in OS X.

A bit more googling turned up the security command. In particular, the dump-keychain command. Finally, running this spat out the password I was after:

  % security dump-keychain -d ~/Library/Keychains/login.keychain

At this point, I found out that it was the password for my old wireless network, which I’d just stopped using. A closer inspection of my login keychain revealed another “AirPort network password” which just happened to be for the new network. Ah well, at least it surrendered itself willingly.

From googling, it appears that many other people have been unable to recover stuff in their system keychain. So this is good stuff to know.


Unicode Depresses Me

Perl is meant to have reasonable Unicode support. So why do I still have to write this at the top of a test?

  use utf8;
  use Test::More 'no_plan';
      my $Test = Test::Builder->new;
      binmode( $Test->output,         ":utf8" );
      binmode( $Test->failure_output, ":utf8" );
      binmode( $Test->todo_output,    ":utf8" );

I would have thought that adding the -CS flag to the #! line would have fixed this. But that doesn’t do it. Ah well, I’ve filed a wishlist bug: RT#21091.


Art in the garden

Yesterday, I went with a few friends to see Art in the garden at Hillier Gardens. I’ve put up the photos on flickr.

Bee & Yellow Flower

I can assure you that despite my utterly inadequate photography skills, the gardens are definitely worth a visit. They’re still in really good shape, despite the long hot summer. And the artwork on display is always interesting and beautifully sitatuated, even if it’s not always to your taste. Definitely worth a visit.

The only trouble is the distance to travel. If you’re near southampton, it’s great, but unfortunately it’s a 2 hour drive from Brighton. Along that deservedly hated road, the A27.


Flickr Favorites

I have been steadily accumulating favorites in flickr for some time now, thanks to the lovely feeds that they offer. But I want them as backdrops for my desktop. I didn’t spot anything else with my 30s of googling, so I wrote a small script to dump my favorites into a directory.

I was pleasantly surprised by how easy the flickr.rb library is to get along with. It’s really trivial to use. I suspect that this is a by product of the flickr api design.

My main disappointment was realising how few people make the full size photos available. In the end, I limited the downloader to photos that are at least 600 pixels in width, otherwise I just ended up with very squished blobs on my backdrop.

The results are worth it. Each time I look at my desktop I now have a fresh picture in it. It makes me smile a lot anyway, and that can’t be a bad thing.

Uncategorized api change

I’ve just been caught out by the api change (they’ve just switched off the old api). This broke the backup script I use. It’s a nice script, it downloads each days bookmarks into a sqlite database, ready for reuse.

Anyway, this is the patch I wrote to make it use the new api (over SSL) instead. BTW, top marks to the people for using basic auth instead of coming up with their own scheme. Basic auth + SSL is a really nice, simple way of doing things.

  --- /home/dom/delicious-backup.rb.orig    Fri Aug 11 09:00:39 2006
  +++ /home/dom/bin/delicious-backup.rb    Fri Aug 11 09:09:19 2006
  @@ -26,11 +32,29 @@
                'values (?, ?, ?, ?, ?);'
   insert_tag = 'insert into tags (hash, tag) values (?, ?);'

  -xml = Net::HTTP.start('') { |http|
  -    req ='/api/posts/all', {'User-Agent' => agent})
  +require 'net/https'
  +http ='', 443)
  +http.use_ssl = true
  +http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  +xml = http.start { |conn|
  +    req ='/v1/posts/all', {'User-Agent' => agent})
       req.basic_auth(user, pass)
  -    http.request(req).body
  +    resp = conn.request(req)
  +    raise resp.inspect unless resp.kind_of? Net::HTTPSuccess
  +    resp.body
  +Dir.chdir(ARGV[1] || ENV['HOME'] + '/libdata/')
  +# Clean anything over 30 days.
  +thirty_days_ago = - (30 *(24*60*60))
  +Dir["*.db"].each do |name|
  +    f =
  +    if f.mtime < thirty_days_ago
  +        File.unlink(name)
  +    end

   db_name = ARGV[0] ||"%Y-%m-%d.db") { |db|

That’s also got my backup expiry bits in, which is handy, but could probably be done better. BTW, that ruby idiom of indexing the Dir class to run a glob is freakish.


Rails Security Hole

Working round the Rails showstopper.

  • (pdcawley)++
  • (svk)++

I now have the fixed version of typo (soon to be 4.0.2), around an hour after it was committed.

As to the whole “full disclosure” thing by the rails team? They handled it pretty badly. As somebody else commented, it didn’t work for OpenBSD a while back and if anybody could do that, OpenBSD could.


V for Vendetta

I’m really glad that Jeremy enjoyed V for Vendetta. Like him, I went in to see it fully expecting to see a much loved comic book butchered into something unrecognizeable. It wasn’t. It was changed, but for reasons that worked better in the medium of film (mostly).

It did amuse me greatly to see John Hurt in the role of Chancellor Sutler, almost exactly the opposite role to that he played in 1984—Winston.

I completely understand why Alan Moore wanted to be disassociated with the film. After all, he’s been burned badly before. He now takes an absolutist, no-compromises view. But you can’t just take something from graphic novel to screen without changes. However, this doesn’t prevent me from thoroughly enjoying his works. 🙂