[tpm] testing; using eval to catch an error in a module

Madison Kelly linux at alteeve.com
Sat Aug 29 11:22:34 PDT 2009

Adam Prime wrote:
> Madison Kelly wrote:
>> Hi all,
>>   I've been working on a test script for my module suite/hobby and 
>> I've been having a bit of trouble. I want to trap errors in the module 
>> without killing the test script. I was hoping I could do something like:
>> eval {
>>     $an->foo("some Illegal arg");
>> }
>> if ($@)
>> {
>>     print "The module 'die'ed as planned and emitted error: [$@]\n";
>> }
>> else
>> {
>>     die "Testing of module death failed as [$@] wasn't set.\n";
>> }
> That seems like it should work, provided that whatever your $an is 
> actually die's if it's given an illegal argument.

$an is a handle to my module. The method called (foo) checks the sanity 
of the argument and 'die's if it's illegal. It does this using an 
internal error handling method that calls the actual die.

In practice, the test.pl script exits at that point. =/

>> I think this is because it's not a compile time error, but the perldoc 
>> for eval shows trapping for a divide by zero error, which as I 
>> understand it would trigger on execution, not compile.
>> Am I missing something fundamental? Should I be approaching my testing 
>> in a different way?
> It doesn't look like you're using any of the existing perl testing 
> stuff, which i'd recommend.  I'm not all that familiar with it myself, 
> but i know that michael went to schwern's testing workshop at yapc this 
> year, so he might be able to point you to some good starting points.
> this seems like it might be a decent start though:
> http://books.google.ca/books?id=o9xWJtvhVZ4C&dq=perl+testing

I'm pretty sure I'm not doing it the "proper" way, but my Googling 
hasn't yielded much yet. The most I've been able to gather so far is 
that the "normal" way is to have a 't' directory and in there have a 
'test.pl' that 'make install' can call.

At this point though, I've not really worried about having a 
Makefile.pl, but I'm hoping I'll have it structured well enough that I 
can, pardon the pun, make it later. I know, I know, I should do it all 
now. This is my first real stab at a totally properly built module 
(dedicated PODs, testable, object oriented... I am a little overwhelmed 
with my little learning project as it is. :)

Thanks for that link, it looks good. A few years out now, but I don't 
suspect that testing is a rapidly changing topic. :)


More information about the toronto-pm mailing list