my $errmsg = eval(SubCall($silly));

There are two use cases of eval: eval BLOCK catches exceptions, and eval EXPR takes a string as the source of a Perl program, and executes it.

You've accidentally used the latter, but you should be using the former. If you write eval(SubCall($silly)), the call to SubCall is not influenced by the eval at all, but rather the return value of the SubCall will be used as a string to be interpreted as Perl code.

The correct solution is to just use the BLOCK form of eval:

my $success = eval { SubCall($silly); 1 }; ok !$success && $@ eq $errmsg, 'died with the right error message';

In Perl 6, the eval BLOCK form has been renamed to try, so there's less potential for confusion. It also works as a statement prefix without any block, so you could write

my $success = try SubCall($silly); ok $!.defined && $! eq $errmsg, 'died with the right error message';

In reply to Re: Testing error handling that calls "die" by moritz
in thread Testing error handling that calls "die" by davies

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.