Dear all wise Monks of the Perl Code,

i have a question that is disturbing my inner confidence.

I want to eval a piece of code. So with an eval I can make sure that an error in the code dont let me script die. But what if the code enter an infinite loop? Is there any way (except modifying the code that is evaled) to stop it My script so far:
my $text = read_some_file_or_else(); eval $text; if ($@) { print "Error in eval: $@"; }

Now I want to give the text to be evaled a certain time to execute and break it of if it not ended after this time. Because the code can be anything (contains maybe an infinite loop or something else that can hang). I cant run it as a separate process, because it needs the context of my script. Anyone have any idea ?

Bless Perl and all other things that are good and righteous,

Andre

In reply to Stopping code in eval by Outaspace

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.