Only if the code you are executing "dies" or throws a warning, in the perl sense, would eval return something in $@.

But as I wrote, there are cases where the code in the eval can die, and $@ doesn't get set (Update: or to be more specific, gets clobbered so that it's no longer available after the eval). A simple demo of this bug:

use warnings; use strict; sub Foo::DESTROY { eval {} } eval { for (bless {}, "Foo") { print "Hello"; die "KABOOM"; } }; print "<$@>\n"; # Where's the kaboom? There was supposed # to be an earth-shattering kaboom!

Even on Perl 5.26, this only prints "Hello<>". That's why the eval { ...; 1 } or do { ... }; pattern is better - choroba showed how that pattern still works even in the presence of a similar bug on Perls <5.14 here.

If the routines you are running return a status, then you want to catch that status coming back from eval

I would suggest writing it this way, where of course 3+4 is a stand-in for something more complex (and it may also be a false value):

my $status; eval '$status = 3+4; 1' or warn $@||"unknown error";

In reply to Re^2: eval failure does not set $@ by haukex
in thread eval failure does not set $@ by wanderedinn

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.