Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Running Perl in an Expect loop

by haukex (Archbishop)
on Dec 15, 2016 at 07:40 UTC ( [id://1177824]=note: print w/replies, xml ) Need Help??


in reply to Running Perl in an Expect loop

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

Replies are listed 'Best First'.
Re^2: Running Perl in an Expect loop
by StoneLeopard (Novice) on Dec 15, 2016 at 13:59 UTC
    Hi Hauke, what I am trying to build is a preprocessor for a format that has embedded free-form Perl within <% ... %> limiters. This in itself is easy since I can simply open(|perl) and send stuff to it, but the second quirk is that there is also a second level of preprocessor macros following Verilog convention (e.g. `include FILE). The corner case that is hard to handle is `include <%$file%> which implies that I need to first send it through Perl, then include the file, which may internally have more Perl :( This is why I was trying to set up a bidirectional link to a Perl process. I'll look at some of the other suggestions in the thread. Thanks Pankaj

      Many of the templating systems can do the replacement for you. Why do you want to talk to a separate Perl process at all if there is eval?

      # First stage: sub process_perl { my( $code ) = @_; my $res = eval $code; my $err = $@; warn "Whoops. Caught error [$err] for code [$code]" if $err; return $res; }; $template =~ s!<%([^%]+)%>!process_perl("$1")!gse;

      If you want/need to do a second round of replacements then, do it afterwards:

      $template =~ s!\binclude\b ([^\s]+)!process_include("$1")!gse;
        The issue is that the file syntax is *not* Perl, i.e. there can be things like this:
        CHAIN<%=$chain_num%>_EN [<%=$lsb%>:<%=$lsb++%>] = 1'h0;

        Even the format spec document suggests this approach "wrap all non-Perl chunks with prints, leaving the code snippets as is, and let Perl evaluate the whole thing".

        The reason a simple eval() doesn't work is because there is Perl variable state that needs to be honored across the entire file (e.g. $lsb in the example above is set to 0 at the start and incremented a number of times through the file. Hence, we will need a full Perl process to evaluate the whole file.

        In any case, after understanding the REPL issue, I broke up the processing into 2 sections:

        * send the entire file to the Perl child process, and close the child's input filehandle

        * then read back the generated output in a while loop

        * if we encounter an "include" statement, then recursively restart the process with the new file.

        This works perfectly. Thanks for all the suggestions.

        Pankaj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177824]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found