Sunday, May 31, 2009

My heretical love for compile time safety...

I have to admit, as a first blog post, this one may not be the best to introduce myself. I program in Perl, every day, mostly for work, but it's cool work none the less. I work in the financial industry, with nearly 13% of the US Stock market coursing through my models . We use lots of Perl to get lots of things done. Having only really seen Perl used in a web environment, I was blown away to see what Perl is really capable of.

I have always been a "use strict" guy, like many of you who would read this I have the t-shirt from thinkgeek with that on the front. My current position introduced to a module called "fields". It took me a while to appreciate it, and it was really only after I started writing code where I wasn't using it did I realize how awesome it is.

The point of my post is, quite simply that Perl fills a niche that no other scripting language (that I have seen) is capable of doing. It provides compile time safety on variables, as well as attributes of objects. People have accused me of having flimsy testing suites, and my only reply to that is that my codebase is so incredibly big (and most of it inherited from previous engineers) that we MUST know about the possibilities for a mistake, and are willing to pay any price to find out before we deploy. This simple validation of any changes we make drastically reduces any errors, and provides a cushion for any code path not covered by a regression or unit test.

I'd really like to be able to have compile time safety of methods, as well as their arguments, the closest way to seem to be able to do this, typesafety doesn't work at all for me, on my mac or on a linux box. I'm told that Perl 5.10 (which we haven't upgraded to yet) has a new method resolution system, I'm hoping there may be a way to override the whole thing and disallow sub-routine generation at runtime so I can do compile time checks. It may be just one big pipe dream, but we'll see.

So there you go. Fields is awesome, and it looks like this whole Moose thing kind of glosses over this, so I imagine that Perl 6 will as well, (if I'm wrong, please point this out!). Hash::Util::lock_keys is not the same. I want to do "perl -wc" and see my errors, right now..

Example:



use strict;
use warnings;
use Hash::Util qw( lock_keys );

my %hsh = ();
lock_keys(%hsh, qw(x y z));
$hsh{'A'} = 1;



This passes "-wc" with no problems...

We prefer a compile time failure to occur, like this:



package FieldTest;

use strict;
use warnings;
use fields qw(a b);

sub new {
my FieldTest $self = shift;
unless (ref $self) {
$self = fields::new($self);
}
return $self;
}

package main;

use strict;
use warnings;

my FieldTest $tmp = new FieldTest();
$tmp->{'c'} = 'whatever';

No comments:

Post a Comment