Categories
Uncategorized

Busy Week

Last Wednesday, I found out that the Brighton Comedy Festival. Unfortunately, it had already started. It’s a shame it seems to have been under promoted this year—there are some really good acts on (plus I missed going to see Marcus Brigstocke). I immediately booked two tickets for Sue Perkins the same evening.

Sue put on an excellent show entitled “The Disappointing Second Show”. Even though it was all about disappointment, it didn’t seem that way. She creatively meandered over large swathes of territory (and extremely loud hecklers), including a very funny discussion of what would actually be on a pink pound…

On Thursday evening, I wandered into the local borders to see Robert Lacey talking about history in general and his latest book in particular. I really find his approach to history excellent for someone like me who detested the subject at school. Concentrating on the narrative makes remembering things much easier.

On Friday, I went to see Fear and Loathing in Las Vegas at the Duke of Yorks, at midnight. I love Terry Gilliam’s works, and this was no exception. Watching a room full of people turn into lizards can’t be beat. And a good cast helps things too (Elijah Wood scares the bejesus out of me after Sin City and this didn’t help matters).

Saturday was mostly spent dropping things on the floor (late nights don’t agree with me much any more). But on Sunday, we took off to see Rich Hall at the Corn Exchange. A grizzled, sarcastic american who swears a lot. It was obviously funny. Especially when he started on about shooting gophers in Montana. But what really surprised me by the end of the performance was the sheer humanity that was hiding underneath the prickly veneer. I came away hugely impressed.

Monday night was completely different. I partook in the first Brighton Coder’s Dojo. Jez Nicholoson and Tom Hume have already written it up quite well. I enjoyed it, although the 5 minute limit really became very clear when I erroneously tried to track down something in the debugger.

Yesterday, I was about to go out to the latest Café Scientifique, but… My central heating went wrong instead. So I had a lovely evening in with the plumber. Ah well. Time for a break.

Categories
Uncategorized

XML::Genx 0.22

I’ve released XML::Genx 0.22. There are no functional changes, just a couple of minor bugfixes in order to ensure that it works on Windows correctly.

For some time now, I’d been trying to get XS modules compiling on Windows correctly under ActiveState Perl, all to no avail. But now, thanks to the wonder of Strawberry Perl, I’ve actually been able to build and test the module all on my own. I am hugely grateful to the authors for putting together Strawberry Perl. It’s a huge boon for developing Perl on Windows (Not that I diss ActiveState; they’ve also done a good job, but they’ve gone in different directions).

Categories
Uncategorized

Shell Scripting

I kind of like shell scripting. It’s quirky, frequently ugly, but it’s damned useful. Like Perl, it annoys me when I see things which could be written in a better manner. One persistent example is abusing the if statement.

  if [ "x$machine" = "x" ]
  then
          echo "not in ~/.machines adding"
          echo $1 >> ~/.machines
          source ~/.bash_aliases
  fi

What’s the problem here? That little “x” in the test. It’s not necessary and hasn’t been since the late 80’s. There used to be a parsing bug in shells which meant that an empty argument got removed. So you couldn’t say [ "$machine" = "" ] because you’d end up comparing $machine to ].

But this got fixed, a long time ago. Really. Unless you’re working on an aging System III box, you shouldn’t worry about this, it’s just Cargo cult programming.

So the test can be just [ "$machine" = "" ]. That’s better. But not good enough. Testing for an empty string? The test command (aka [) provides a -z operator for that! So goes down to [ -z "$machine" ].

In this particular case, we’re explicitly targetting bash, though. One of the nice features of modern POSIX shells like bash is that they provide conditional expressions, which have different parsing rules. So you can say [[ -z $machine ]] and not worry about the lack of quotes.

Lastly, I should point out that I missed off the first line of that extract.

  machine=`grep $1 ~/.machines`

With that in place, you can see that we’re not actually using $machine anywhere in the function. It’s purely in place for the if statement. Which means you can simplify this even further. if takes a command as it’s argument. So why not just give it the grep command?

  if grep -q $1 ~/.machines
  then
    echo "not in ~/.machines adding"
    echo $1 >> ~/.machines
    source ~/.bash_aliases
  fi

I had to add the -q flag to shut grep up; we’re only interested in the return code, not the output.

Does this matter? Probably not much. But like all programming languages, it’s a lot easier to read when you use it idiomatically.

Categories
Uncategorized

Sussex Geek Dinner

I went to the Sussex Geek Dinner last night. Tristan Roddis was talking about Plone. I was initially very skeptical, as I had a bad Zope experience several years ago. I’m extremely glad to see that development has come on a long way. Whilst there’s still a very steep learning curve, you can now actually use source control with it, which was still not really done when I last looked (circa 1999). Anyway, it sounds really powerful when you get to grips with it, which Tristan’s company certainly have. I also pointed out Varnish to Tristan as he mentioned using squid in front of a CMS. 🙂

I also found out that he’s working in the same building on the floor above me. Which is really the point of events like these. It’s all about bringing the local tech community together so we get to know each other better. I was really pleased to bump into a few people I hadn’t seen in a while last night. We had a great talk after the main show—mainly about photography.

Just after 10, a few of us wandered over to the farm meet in another pub. I bumped into even more people there and had lots of deliciously geeky talk. It turned out that Sevan had just been interviewed on bsdtalk two hours previously about his work on the Brighton Chilli hotspot project. I was quite shocked to find that interview downloading to my ipod when I got back home. Good work Will Backman!

Categories
Uncategorized

Unicode in Rails

Unicode in Rails takes a step further today, as ActiveSupport::MultiByte is committed to the edge (r5223). More information is available over at fingertips, including a neat demo video. This should really help people who need proper Unicode support. There’s no excuse to not use UTF-8 now!

Categories
Uncategorized

Varnish

I listened to an interview about Varnish this morning. It was on the excellent bsdtalk podcast. Varnish is an attempt to write a high performance HTTP accelerator (reverse proxy). It grew out of frustrations with the performance of squid. Because it’s focussing on a much smaller problem space, it’s much simpler to use. Anybody who’s wrestled with squid config files before will sympathize.

It was primarily written by phk, one of the major FreeBSD contributors.

But what I found really interesting was the little nugget about the configuration of Varnish. They wanted the configuration to be a DSL, so you can easily do things like make caching decisions based on HTTP headers and URL matching and so on. But the implementation surprised me: they compile the config file to C, compile it to a shared library and then load it into the Varnish process. So it’s damned fast, as you don’t have the overhead of interpreting bytecodes like you would with embedded Ruby or Perl.

It still makes me a little nervous about the availability of compilers on a production server (it’s generally considered bad security practice). But you can always compile on an identical machine elsewhere.

Overall, it seems like an interesting tool for helping web sites to perform. I’ll try to give it some proper attention as soon as I get a chance.

Categories
Uncategorized

Controller Plugins in Rails

I want to write a Rails plugin, which adds a controller to a rails app. It seems like it should be easy, plugins are fairly flexible after all. After a couple of experiments, I concluded that you need at least the following defined in your plugin:

lib/foo_controller.rb
1
2
3
4
5
  class FooController < ActionController::Base
    def hello
      render :text => 'hello world'
    end
  end
lib/foo_helper.rb
1
2
  module FooHelper
  end

But there’s still a serious problem. It only works on the first request. After much mucking around in routing.rb, I’ve found out why. It turns out that when a request is finished, dispatcher.rb clears out all the controllers, so that routing.rb will reload them on the next request (only in development mode). The problem is, that the controller in the plugin doesn’t live in one of the “trusted” paths1, so can’t be reloaded. So, on the second and subsequent requests, you get a routing failure.

There’s a quick and easy way around this.

lib/foo_controller.rb
1
2
3
4
5
6
7
  class FooController < ActionController::Base
    class << self
      def reloadable?
        false
      end
    end
  end

But that means that you have to restart the web server each time that you change your plugin, which certainly isn’t ideal. At this point, I started thinking about monkey-patching how the reloading works, but a gag reflex caused me to take a step back and think differently.

One minor tip: running rake rails:freeze:gems makes it much easier to dig into the rails source code. Mostly because it all becomes immediately accessible from within textmate’s “Go To File” command.

Looking around the web, I can’t find much information about controllers in plugins. One answer appears to be the rails engines plugins. But it appears to be more heavyweight than I’d like.

However, nuby on rails mentions an interesting alternative.

You can’t directly write a controller plugin, but you can write a generator that copies a controller to your app/controllers directory.

The more that I think about this, the more it makes sense to me. It ensures that magic URL namespaces don’t just “appear” in your application. It also provides a point for overriding aspects of the actions I wish to provide. In addition to all that, it’s just plain better separated so easier to test.

And I haven’t even begin to think about how the views are going to work…

1 app/, lib/ and components/ according to safe_load_paths.