Future Perl
This is mostly stuff I’ve gathered from this talk and updated slightly.
First off, have you ever tried to teach a programmer perl? I have. Note this:
sub foo {
my ($self, $bar,$baz) = @_;
#...
}
# or often
sub station {
my $self = shift;
#...
}
The following is more palatable to most coders (also I like it better:)
use Method::Signatures::Simple;
method foo($bar, $baz) {
#...
}
method station {
#...
}
That’s right, no fiddling with @_ at all. Sweet! Also note: this is not implemented with sketchy source filters. It’s quite robust. There is also MooseX::Method::Signatures (and MooseX::Declare which uses that) which can do even more, like defining named and optional params and type constraints.
I’ve already mentioned IO::All, so I’ll just say that it’s been endorsed by Matthew S. Trout, so you don’t have to tell me that the Right Way is to use some crufty C based interface or some weird old perl module that has capital letters in the functions and method parameters.
Next up, Moose::Autobox. I’ve also mentioned autobox, which this module uses, but Moose::Autobox goes further and defines some roles that your classes can also use to make them act like arrays etc. I wouldn’t be surprised if DBIx::Class 0.09x used some of these roles. Anyway, here’s a real example:
use Method::Signature::Simple;
use Moose::Autobox;
method criticisms {
return {
data => $self->files_criticized->values->map(sub { @{ $_->{criticisms} } })
};
}
Not quite Perl 6/Ruby, but still much clearer than the original (at least to this function programmer!)
And the of course there is the recently released (four months ago!) TryCatch. I haven’t used this module at all, but I look forward to it. It could really streamline some of the stuff we do at work.
Lastly, CLASS is a tiny nicety. All it does is replace __PACKAGE__ with CLASS; the code looks cleaner with it and it’s certainly shorter and easier to type.
I think we’ll be seeing a lot more of this kind of stuff in the near future. Devel::Declare seems to have mostly matured, so these modules will probably continue to crop up.
Anyway, woohoo! The future!
Posted Thu, May 7, 2009If you're interested in being notified when new posts are published, you can subscribe here; you'll get an email once a week at the most.