G'day stevieb,

It's your module and you can test it in whatever way you want; however, I'd have a problem with the separation of the eval block and $@ several lines later.

Here's a real-world ($work) example I wrote a few years ago that similarly tests for failure. In this instance, it checks that an abstract class can't be instantiated. I just grabbed it from git and only changed the class name to Abstract::Work::Class; otherwise, this is a verbatim copy of 01-abstract.t:

#!perl -T use 5.016; use warnings; use Test::More tests => 3; use Abstract::Work::Class; my $obj; my $no_instantiation = 0; my $eval_message = ''; eval { $obj = Abstract::Work::Class::->new(); 1; } or do { $eval_message = $@; $no_instantiation = 1; }; ok(! defined($obj), 'Test potential object remains undefined'); ok($no_instantiation, 'Test no instantiation of abstract class'); ok(index($eval_message, 'FATAL! Attempt to instantiate an abstract cla +ss.') == 0, 'Test error message from attempting instantiation of abstract clas +s');

I normally attempt to capture the value of $@ as the first statement in '... or do { ... };'.

As a testament to the robustness of that code, it's from one of the core modules of a large 30+ module framework. The framework has been modified, extended and enhanced many times, and built on many platforms, over the last four years; 01-abstract.t was last modified in May 2018. It's a solid solution and you're welcome to adapt it to your needs if you want.

— Ken


In reply to Re^5: Can I catch a die from someone else's module? by kcott
in thread Can I catch a die from someone else's module? by bartender1382

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.