You can think of the two versions of eval as completely different 'functions' (although the block form is not a function - it's a flow control statement(?)). Perhaps they should actually have different names, because they do completely different things.

String eval takes a string, interpolates varibles, and executes the string as Perl code. You use this to delay the evaluation / parsing of whatevers in the string to run-time. It allows you to build Perl programs on the fly and run them. The reason it's dangerous is because the temptation is there to insert user input into the string, effectively allowing the user to execute arbitrary code.

Block eval doesn't have this issue because the code inside it is parsed at the same time as the rest of the code in the program. Therefore, user values can't be inserted into the code.

The primary use of block eval is to catch die / croak, etc. Again, this is another slightly strange naming issue in Perl - in this case, your program won't actually die, it just provides a way of exception handling (i.e. passing error information between levels of the application). In most other languages, the block eval is called 'try', and has a corresponding block called 'catch'. In Perl (5 at least, this changes in 6), the same functionality is achieved by eval{} / if ($@).

I suggest you read the eval POD for full details.

In reply to Re^5: testing a croak by Mutant
in thread testing a croak by arcnon

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.