String eval does what it implies on the packet - it evaluates a string as though it were Perl source code. Given that that string could include user input there is the very real possibility that malicious user supplied code could be run with whatever credentials your executing script has. Often that's a pretty wide open door to do nasty stuff.

Block eval on the other hand is much more like the try / catch blocks provided in other languages. It allows a way to manage execution errors generated within the eval. For example:

my $ok = eval { # code that might call die to signal that something has gone wrong ... return 1; } or do { # error handler code my $error = $@; ... };

Any die executed within the eval block is handled by the do block allowing error recovery or at least nice error reporting. die and block eval is a very nice way to handle errors without having to hand explicit success back from called code. That helps make code much cleaner and makes it easier to manage error handling correctly.

Premature optimization is the root of all job security

In reply to Re: The safety of string eval and block eval. by GrandFather
in thread The safety of string eval and block eval. by TrixieTang

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.