Archive for the ‘Perl’ Category

Testing App::Cmd

Wednesday, January 9th, 2008

As I mentioned last week, I’ve been working on a little reading list management tool. I’m using App::Cmd as the framework, which has been a somewhat frustrating but ultimately worthwhile choice.

I first learned of App::Cmd at the Pittsburgh Perl Workshop, through a talk given by the module’s author Ricardo Signes. The slides from that talk are about the best App::Cmd documentation out there, although App::Cmd::Tutorial also has some useful bits, and the module documentation itself isn’t bad by any means, just a little light on example code.

For BookList I didn’t do a lot of up-front design work — I was basically designing as I went and learning App::Cmd at the same time. As a result, I ended up implementing several commands with only the most rudimentary tests — basically, if the command module loaded, I was happy. Since BookList is getting towards a 0.1 level of functionality and I’d like to release it, I spent the past couple days renewing my appreciation of “test first” development by going back and writing all the tests I skipped over the first time through.

Because of the way the framework works, testing App::Cmd code is a bit different than testing library-level routines. The talk I pointed to above has a section on testing (around slide 115, for those of you who want to check it out), which references a nifty looking module called Test::App::Cmd. Unfortunately, the module doesn’t appear to be publicly available yet. (This might have been mentioned at the talk; I don’t recall. Any status updates are welcomed.)

After poking around on CPAN, trying to find something to provide some scaffolding for my tests, I ended up finding a module called Test::Output. I used that and the sample test code in the talk to write a bunch of tests that looked like this:


use Test::More     qw/ no_plan /;
use Test::Output   qw/ stdout_from /;

use Booklist::Cmd;

my $error;
my $stdout = do {
  local @ARGV = ( 'authors' );
  stdout_from( sub {
    eval { Booklist::Cmd->run ; 1 } or $error = $@;
  } );
};

like $stdout , qr/^###  #bk  author/ , 'see expected header';
ok ! $error;

and while that mostly worked out okay, I ran into trouble with some of my error-handling code. When writing libraries, especially for my personal use, I tend towards the low-budget “croak() and let the caller deal” method of exception handling. But for something that’s going to be more application-level, I wanted to print messages to STDERR and then exit() with a non-zero status. This doesn’t play well with Test::More or Test::Output, neither of which trap calls to exit().

After a bit more CPAN searching, I found Test::Trap which is excellent for testing App::Cmd code. The above code rewritten to use Test::Trap looks like this:


use Test::More    qw/ no_plan /;
use Test::Trap    qw/ trap $trap /;

use Booklist::Cmd;

trap {
  local @ARGV = ( 'authors' );
  Booklist::Cmd->run;
};

$trap->leaveby_is( 'return' , 'exit normally' );
$trap->stdout_like( qr/^###  #bk  author/ , 'see expected header' );
$trap->stderr_nok( 'nothing on stderr' );

That second code block is a lot cleaner and easier to understand than the first one (not to mention being fewer LOC while doing an additional test), and the accessor/comparison methods (leaveby_is, stdout_like, etc.) from Test::Trap are both easy to write and (if you know anything at all about idiomatic Perl testing code) trivial to understand.

I’m going back now and rewriting all my previous Test::Output tests to use Test::Trap — but I’ve also learned another lesson here: my last test is currently failing, because I wrote it beforeimplementing the command it tests.

More Perl Modules

Monday, October 9th, 2006

Stuff that I’ve come across that I should take a longer look at:

Perl conference

Monday, September 25th, 2006

Friday, after work, NowThis, a coworker of his, and I all drove up to Pittsburgh to attend the Pittsburgh Perl Workshop. The conference was outstanding — well organized, good speakers, good food, and cheap — NowThis and I were in on the $20 “early” registration fee (we both registered less than a week before the conference started) and the coworker was a speaker, so his fee was waived.

Talks to look for: Beth Skwarecki’s “Make Your Database Work for You”, about getting all that database-specific stuff out of your code and into, you know, the database; Dave Rolsky’s “Perl Modules for Exceptions, Parameter Validation, and Logging”, a Grand Tour of a particular slice of CPAN (lots of notes from this one); and Andy Lester’s “Preventing Crisis: Project estimation and tracking that works”, which contains details of A System that would dovetail nicely with the one in Time Management for Systems Administrators.

I’m sure that there were other good talks that I didn’t get to; the list above are merely the ones that I was in that merited a mention. If you’re a Perler in the area, you really should consider heading to Pittsburgh next year for the next PPW…

Lock down your Perl

Tuesday, July 11th, 2006

If you want a bit more B&D in your Perl programming, and you use Emacs, you should probably look at perltidy-mode and perl-lint-mode.

Perl Hacks

Tuesday, June 27th, 2006

I picked up Perl Hacks at the book store the other day. I’m not going to do an in-depth review, mainly for lack of time — all I will say is that if you’re a reasonably serious Perl programmer (i.e., if at some point somebody has given you money because you knew some Perl) and you’re interested in learning more about it, this is a good use of your thirty bucks. You will get at least that much value back out of it.

For me, the book had paid for itself by the end of the first chapter, by introducing me to Pod::Webserver. This handy little dingus provides a local high-port web server for all the Perl documentation that’s stored on your computer. So, doing some coding on your laptop in a wireless-free zone and want an HTML view into perldoc? Fire this baby up and it’s all clickity-click action. Completely worth thirty smackers, that tip, at least IMO.

QotD

Sunday, June 18th, 2006

From <87lks6jvij.fsf@toddler.lart.ca> (and that should be enough Clue for you to recognize the newsgroup if you’re going to…):

“Will I get it all figured out eventually? Yes. Will I be sorely tempted to carry a gigantic python book around at all times, so the next time someone talks about perl being executable line noise I can hurl the book at said pythoner? Yes, yes I will be so tempted.” — Luke Kanies, hates-software.com

Update: I note that hates-software.com sports a weblog. Noted for the time, Real Soon Now, when the feed reader software is up and working…

Catalyst organization changes

Tuesday, May 2nd, 2006

Reading between the lines, there have been some major shakeups in the Catalyst dev team organization. I haven’t been doing too much Catalyst stuff recently, because the pace of change of the project was just too high for me to keep up with. It sounds like one of the side effects of this shake up might be a slowdown, which would be nice.

Slides from Catalyst talk

Wednesday, March 8th, 2006

Such as they are… The slides are minimal, but I thought the talk went basically okay. Note to self: practice the demo portion more next time, and realize that when you’re typing in front of an audience, you need to slow down and focus more on the screen, less on the crowd.

Catalyst/Perl MVC framework talk tomorrow night

Monday, March 6th, 2006

Reminder for the local-area peeps: I’m giving a talk on Catalyst and Perl MVC frameworks tomorrow night at the DC Perl Mongers meeting. (Ignore that the site has the August 2005 meeting notice up still — the time, location, and directions are still accurate…)

Hope to see people there — assuming I get the talk finished, that is… 8^)=

Update: Keith Ivey updated the web site — thanks Keith!

Emacs as Perl IDE

Sunday, March 5th, 2006

Notes on perlnow.el