viernes, 4 de diciembre de 2009

Perl December

Yay! It's december. At first, this doesn't seem that exciting, but every perl hacker knows a few facts related to december.

  1. Advent Calendars are here again
  2. Christmas is near.
This week I've been coding some perl, and revisited some practices/modules I've been some time without using.

I'll try to explain some of them, that I found particularly useful.

Starting a project

Although this is a small app, and could be thought as a script, following the "always code as if you uploaded the code to cpan", I used Module::Starter with Module::Install. I could have used Dist::Zilla, that seems to be more DWIM-ISH , but well, I already had the "magic" module-starter line, so

module-starter --module=My::App --author="Raimon Grau" --email="" --builder=Module::Install

Object oriented perl.

I used a simple OOP interface. As noone at $work follows the perl world , I decided not using Moose, and used plain OOP. I used a bit of sugar with Class::Accessor, so attributes accessors and mutattors could be autogenerated.

use base 'Class::Accessor';
__PACKAGE__->mk_accessors(qw/attr1 attr2/);

When there is some error, for example, opening a file (it may not exist), I couldn't just die (dying from a lib is not nice), nor warn, so I used Carp module, to get more informative info about when the code errs.

Testing and Pod Coverage

I did some tests to validate my code (at least some basic things), and also ran Pod coverage (module-starter added the test automatically).

In vanilla OOP perl, you can't write private methods (at least, easily), but there's a convention of methods begining with '_' being private helpers.

Pod::Coverage, a tool that ensures you write documentation for all your methods, also follows this convention, so you can leave _functions uncommented. Great way to guide you to best practices. There's also more sophisticated ways to use it.


Having to manipulate CSV files, I had a few options, but ended using Text::CSV__PP.
I needed only basic features so everything went ok with this.


Having to deal with paths and files typed by users is annoying because of trailing '/'. For example, you can easily end with a path like "/home/user//this/isa/pathFile".

Perl provides File::Spec::Functions::catfile to sanitize those cases. Great finding.

To dump structures, I've used Data::Dump, that, prints the smaller output (linewise).

Meanwhile, I've been enjoying both lhp and pp2 Casiano's courses. Seriously, I think it's jewel in the crown on spanish perl courses.

That's all for this post.

Remember, be nice, and fight for your rights. Well, fight for theese rights too. And theese.