Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Running Perl in an Expect loop

by StoneLeopard (Novice)
on Dec 15, 2016 at 01:08 UTC ( #1177816=perlquestion: print w/replies, xml ) Need Help??
StoneLeopard has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks,

I'm trying to spawn a Perl process via an Expect loop. The idea is that the main program would be sending Perl code to the slave, and have it return stuff on STDOUT, which would be received, parsed and acted upon by the main program. I tried to implement this via Expect, but it seems to be deadlocking. Can someone point out what I'm doing wrong?

I'm using Perl v5.14.1 if that makes a difference.

Thanks much. Pankaj

use Expect; # create a pipe to slave Perl process my $exp = new Expect; $exp->raw_pty(1); $exp->exp_internal(1); $exp->spawn("/usr/bin/perl") or die "Couldn't start Perl: $!\n"; $exp->log_user(0); $exp->log_stdout(0); # attempting to unbuffer the slave Perl's stdout test_ipc ("select STDOUT; \$| = 1;\n"); test_ipc ("print \"Got this\\n\";\n"); # close the pipe $exp->hard_close(); sub test_ipc { my $line = shift @_; # send command to Perl $exp->send ($line); # may receive any number of lines in return (or none) my $done = 1; while ($done) { $exp->expect (1, [qr"\r\n" => sub { print STDOUT $exp->before(). "\n"; exp_ +continue; }] , [timeout => sub { $done = 0; print STDOUT "TIMED OUT\n"; +}] , [eof => sub { $done = 0; print STDOUT "DONE\n"; }] ); } }

Replies are listed 'Best First'.
Re: Running Perl in an Expect loop
by haukex (Abbot) on Dec 15, 2016 at 07:40 UTC

    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

      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;
Re: Running Perl in an Expect loop
by Corion (Pope) on Dec 15, 2016 at 08:27 UTC

    See also IPC::PerlSSH, another framework that allows you to run Perl code on a remote connection.

Re: Running Perl in an Expect loop
by salva (Abbot) on Dec 15, 2016 at 07:50 UTC
    There are several frameworks in CPAN allowing the execution of Perl code in other processes (local and remote). Check for instance Object::Remote.
Re: Running Perl in an Expect loop
by StoneLeopard (Novice) on Dec 15, 2016 at 16:46 UTC

    Thanks for all the comments!

    After realizing the REPL problem, I switched over to a simple IPC::Open2 call instead of Expect. But, instead of trying to interact with the child line-by-line, now I'm sending the while file to the child Perl process, closing its input handle, and then reading back the results.

    This does exactly what I needed.

    Thanks

    Pankaj
Re: Running Perl in an Expect loop
by Anonymous Monk on Dec 16, 2016 at 00:05 UTC
    You use Expect to essentially screen scrape output because you do not have API hooks to program against. In this case, you have Perl and you want to interface with Perl. So don't use Expect. Use Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1177816]
Approved by beech
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-07-21 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?