I will do it this way:

use strict; use warnings; my $e = "foo(0)"; eval $e; sub foo { my $i = shift; print "$i foo\n"; if ($i < 10){ $i++; print "calling foo\n"; foo($i); } }

Other than the direct issue others already pointed out, there is two other style things:

  1. $i is not well scoped. You never define a variable with a scope even slightly bigger than its need. In case, $i shall be defined within the foo() function, and be passed as a parameter when it calls itself.
  2. It might seems okay for a demo, but define a sub in a string is not quite a good idea.

Your guess that it has something to do with multithreading is... as multithreading will not help in this case.


In reply to Re: eval strings non reentrant? by pg
in thread eval strings non reentrant? by mutated

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.