Making Perl "Less Bad" 27 Jan, 2006
I've got a little project I'm doing in Perl right now at the office. As a requirement I was setting for myself, I was going to do it in OOP Perl as opposed to "code soup flat namespace perl" and use some sort of tests.
While I have yet to find anything as universal/easy as Python's doctest mechanism, Test::More does provide a nice little set of functions. To utilize them, make sure you have it (one of our versions of Perl didn't come with it), then use syntax something like the following (check the perldoc for it, it's an easy read):
use Test::More tests => 3; # change this number ok(1, "One is a valid answer"); is("blah", "bl"."ah", "Test blah concatenation"); ok(0, "Zero is a valid answer");
I noticed that the perl makefiles seemed to perform a slightly different output that was more concise, so after some digging, located this command (here for future reference, because I'm going to forget).
perl -MExtUtils::Command::MM -e 'test_harness(0);' t/*.t
I also came up with this colorizer for when using just Test::More, not the harness (I haven't tried them together).
#!/bin/bash perl testit.pl 2>&1 | \ perl -pe 's/^(ok )/\033[0;32m$1\033[0m/' | \ perl -pe 's/^(not ok)/\033[01;41m$1\033[0m/' | \ perl -pe 's/^(# Looks like you )(failed \d+)/$1\033[01;41m$2\033[0m/g'
I also spent way too long fighting the use of Error.pm (which is not well documented — that link is the best I can find), which kept giving me the following:
Can't use string ("1") as a HASH ref while "strict refs" in use at /usr/local/share/perl/5.8.4/Error.pm line 183.
The cause was forgetting the semicolon after the end of the try/catch
block.