Virtual Spring Cleaning (part 6 of X) wherein I expose my bad taste for all to see

Part of my motivation for the virtual spring cleaning is that I have many slow-cooking pet projects in which I try out new modules or features that have not yet passed the test of time, at least at the moment of creation. Usually, I only work on these projects when I have both, inspiration and downtime, usually right after Perl workshops. One example of such a project is a prototype of a rogue-like game named App::StarTraders (unpublished) which is intended to become a clone of Elite.

I started App::StarTraders several years ago and it was the first set of modules in which I tried out the signatures feature. At the time, my laptop was not running 5.20, and I did not want to do a reinstallation orgy at an airport, or break my live demos by doing an upgrade of Perl. In the spirit of lazyness, I reimplemented what I wanted from signatures using a source code filter. That reimplementation was intended just as a shim to tide me over until I had a recent enough Perl on every machine where I work on App::StarTraders.

People who are not aware how bad an idea source filters are should just assume that they are a horrifyingly bad idea. On the other side, some amazingly cool things can be done with just a few simple lines of Perl, together with Filter::Simple , which implements the hairy guesswork of finding which pieces of text are Perl code and which are something else.

Basically, Filter::signatures is just one regular expression:

    s!\bsub\s*(\w*)\s*(\([^)]*?\@?\))\s*{\s*$!sub $1 { my $2=\@_;!mg;

which turns

    sub greet ( $greeting, $name ) {
      ...
    }

into

    sub greet {
      my ( $greeting, $name ) = @_;
      ...
    }

There are other modules that don’t use Filter::Simple but B::Hooks to do the same magic with at least a bit more of direct support from within the Perl parser. But as I wanted something light and easy, being able to quickly inject print statements into the filter code was far much easier to debug than fighting with B::Hooks and hoping that it would compile on all versions of Perl I happen to use.

Leave a comment

About Max Maischein

user-pic I'm the Treasurer for the Frankfurt Perlmongers e.V. . I have organized Perl events including 9 German Perl Workshops and one YAPC::Europe.