21st Century Perl (part 1)

Penfold on 2007-12-20T07:34:15

So, here we are: blank slate, new project. The only stipulations from on high are an API to code to, which mandates Perl (good job, really, as that's why we're all here, I hope). In a nutshell, I have to duplicate the essential methods of a certain sprawling God Object, but talking to someplace different. Fortunately, in this context, 'essential' means mostly the ones it originally was designed to provide, not the subsequent decade of accumulated cruft.

I was, when I started this blog, not... wholly enlightened. I knew there was a place I and my Perl needed to be, and I was aware that there seemed to be a lot of people in the same boat, some searching for that place, some dismissing Perl because it didn't seem to be in that place. If you're coming to this blog from the enlightened perspective I started out searching for, none of this should be all that new, really: it isn't rocket science, and it certainly is possible in Perl, just as it is in any other modern programming language. So, let's begin...

...with Unit Tests.

One of the 'scales falling from my eyes' moments I had at $PreviousJob (involving Java and the like, for those for whom it's been so long they can't remember previous posts here) was in the realm of unit testing and the whole Test Driven Development philosophy. The decree came from On High (small company, so only one level up), "Thou shalt write unit tests."

I groaned. " What am I? QA?"

I had to figure out the answer for that one myself, but when I did, it was a Road to Damascus moment, a real 'holy shit, of course' leap. And it's this: 21st Century Perl Revelation # 1:

We all write unit tests anyway.

We just don't necessarily call them unit tests or treat them like it.

Think about it. You write a new piece of code, and then you satisfy yourself it works. First off, by the simple things like 'is it actually syntactically correct Perl', and then you start writing little command-line Perl one and two liners to invoke the code in various ways and see if and how it breaks. It does, so we move on. And a little while later, something makes us refactor the code, and we go back and we write more little command-line Perl one and two liners to make sure it still works.

Those are unit tests. Except that if you're like I was, you never kept the little Perl one liners from one iteration to the next.

Behold, Test::More and all its red-headed stepchildren.

Your repeated invocations of perl -cw My::New::Module?

use Test::More plan_tests => 1;
use_ok(qw/My::New::Module/);

Glory Hallelujah, a unit test. And blow me, if it isn't even less typing: prove -v t/*.t, or even make test. Praise the Lord, I have seen the light! The first step on the road to salvation.

Look at that code for that method you just put in the sub {} declaration for. Ask yourself, how would I check if this works? What is my little Perl one or two liner going to look like? Define "works" for your method, for your module. Write some tests that encapsulate that definition of "works" before you even finish coding the module. Save them under useful names in t/. Run them, every time you change your code.

Testify, brother.


Probe deeper young one

Alias on 2007-12-20T08:31:44

Even better than little programs (and of course unit tests) that you run to see that the code works, is little programs and tests you run in the debugger to WATCH the newly written code and see it working.

And with "pler" (installable from CPAN) it's even LESS typing!

t/02_dosomething.t

pler 2

Re:Probe deeper young one

jplindstrom on 2007-12-20T11:48:30

Confused. Is pler or Devel::Pler the preferred thing to use?

It seems pler is maintained, obsoleting Devel::Pler, but that neede some research. Maybe one of them should go?