Since 5.28, it doesn't matter.

Before, especially before 5.14, $@ could get clobbered, so if eval { ... ; 1 } was more reliable.


There was a time where it was possible for $@ to get cleared or replaced before eval returned.

package Mod { DESTROY { eval { } } } eval { my $x = bless( {}, "Mod" ); die( "xxx\n" ); }; print $@ || "[undef]\n";
$ 5.12t/bin/perl a.pl [undef]

This has been fixed.

$ 5.14t/bin/perl a.pl xxx

So, using eval { ...; 1 } and checking the result of eval was more reliable. And it was promoted as the more reliable solution for this reason. There was an attempt to fix in 5.14, but edge cases were only fixed in 5.28.


Updated to cover edge cases found later, as per choroba's reply.


In reply to Re: canonical doc explaing need for "eval { ... } or do {...}" construct by ikegami
in thread canonical doc explaing need for "eval { ... } or do {...}" construct by LanX

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.