in reply to IPC::Run and subprocess interaction

Two problems.

#!/usr/bin/perl -w use strict; use IPC::Run qw( start pump finish timeout ); my @tok_program = 'cat'; my ($TOK_IN, $TOK_OUT, $TOK_ERR); my $TOK = start \@tok_program, '<', \$TOK_IN, '1>pty>', \$TOK_OUT, '2>', \$TOK_ERR or die "Error: $?;\n"; while (my $line = <STDIN> ) { # Send input. $TOK_IN = $line; pump $TOK while length $TOK_IN; # Wait for output. pump $TOK while $TOK_OUT !~ /\n\z/; print "out: $TOK_OUT"; $TOK_OUT = ''; } finish $TOK or die "returned $?";

Expect also uses a pty, and it might be better suited to control interactive application.

Replies are listed 'Best First'.
Re^2: IPC::Run and subprocess interaction
by xhudik (Initiate) on Jul 15, 2011 at 07:52 UTC
    Thanks ikegami. You are right. Hmm, so my basic mistake was not to put the second pump (for reading back to $TOK_OUT). During my Internet browsing I didn't find any example with 2 pumps.

    Just for the sake of curiosity. $TOK_OUT doesn't seem to be an ordinary variable. If you run the following program:

    #!/usr/bin/perl -w use IPC::Run qw(start pump finish timeout); my $tok_program = "/bin/./cat"; my ($TOK_IN, $TOK_OUT, $TOK_ERR); my $TOK = start [$tok_program], '<', \$TOK_IN, '1>pty>',\$TOK_OUT, '2>', \$TOK_ERR , debug => 0 or die "Error: $?;\n"; while (my $line = <STDIN> ) { # Send input. $TOK_IN = $line; pump $TOK while length $TOK_IN; # Wait for output. pump $TOK while $TOK_OUT !~ /\n\z/; my $msg = $TOK_OUT; chomp($msg); print "out:$TOK_OUT;;MESSAGE=$msg;;END\n"; $TOK_OUT = ''; } finish($TOK) or die "returned: $?";
    You will get:
    out:1. line ;;ENDSAGE=1. line out:2. line ;;ENDSAGE=2. line
    Instead of:
    out:1. line ;;MESSAGE=1. line;;END out:2. line ;;MESAGE=2. line;;END
    Do you know why?

    Once more - thank you!

      During my Internet browsing I didn't find any example with 2 pumps.

      Only one is needed. This would do:

      $TOK_IN = $line; pump $TOK while $TOK_OUT !~ /\n\z/; print "out: $TOK_OUT"; $TOK_OUT = '';

      This one is in the docs.

      $TOK_OUT doesn't seem to be an ordinary variable.

      Change chomp to s/\r?\n\z//.

        yes, this is exactly what I needed - thank you

        The strange thing is that cat program is giving \r\n instead of \n as EOL. For what reason \r is there? Normal(in bash), cat is writing just \n ...

        cheers, Tomas