Hi StoneLeopard,

What is the overall problem you are trying to solve by executing code like this? The reason I ask is because - sorry to be direct - your solution seems over-engineered.

The reason your code isn't working is that the standard perl process does not implement a REPL (Read-Eval-Print Loop), it only executes a script once it has read and parsed the entire file (the exception being BEGIN blocks, but I don't think that's appropriate here either). You can try this out yourself by doing manually what Expect is doing: at the command line run perl, type in some code, and you'll see it won't be executed until you send an EOF.

Now, you could have the child process run a REPL loop (from CPAN, from PerlMonks), but even then the question is, why not do away with the child process entirely and just use eval directly? If you need to capture the executed code's STDOUT, you could use Capture::Tiny.

Remember, executing any code from untrusted sources is a huge security hole!

Hope this helps,
-- Hauke D


In reply to Re: Running Perl in an Expect loop by haukex
in thread Running Perl in an Expect loop by StoneLeopard

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.