Hi Monks, I'm trying to write a simple perl script that would execute program (e.g. cat), and then make some interaction with the program. I'm using IPC::Run since it is able to emulate terminal (pty) which is needed for the interaction (some putting and getting data from the subprocess). My 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], '<pty<', \$TOK_IN,'>pty>',\$TOK_OUT, ' +2>', \$TOK_ERR , debug => 3 or die "Error: $?;\n"; while (my $line = <STDIN> ) { chomp($line); $TOK_IN = $line; $TOK_OUT = ''; $TOK_ERR = ''; print "Input2pump:$TOK_IN\n"; pump $TOK while length $TOK_IN; print "out:$TOK_OUT\n"; }
Basicaly, it should execute cat program and then post each input to the "cat" and wait for the answer. However, the output is not what it was supposed to be :). (The output can differs from time ti time - sometimes is completely empty, sometimes just 1 entry is empty,...) Obviously, I'm missing something but no idea what Any help will be appreciate!
testing file a:
1. line
2. line
The command:
./wrap < a
And the output is:
IPC::Run 0000 0123---7---- [#1(20574)]: debugging fd is 3 IPC::Run 0000 0123---7---- [#1(20574)]: ** starting IPC::Run 0000 0123---7---- [#1(20574)]: opening pty '0' IPC::Run 0000 012345-7---- [#1(20574)]: pty() = ( 4, 5 ) IPC::Run 0000 012345-7---- [#1(20574)]: '/bin/./cat' is absolute IPC::Run 0000 012345-7---- [#1(20574)]: kid to read 0 from pty '0' IPC::Run 0000 012345-7---- [#1(20574)]: kid 1 to read 0 from SCALAR vi +a pty '0' IPC::Run 0000 012345-7---- [#1(20574)]: kid 1 to write 1 to SCALAR via + pty '0' IPC::Run 0000 012345-7---- [#1(20574)]: kid 1 to write 2 to SCALAR IPC::Run 0000 012345678--- [#1(20574)]: pipe() = ( 6, 8 ) IPC::Run 0000 012345678--- [#1(20574)]: kid 1[]'s 0 is my 4 IPC::Run 0000 012345678--- [#1(20574)]: kid 1[]'s 1 is my 4 IPC::Run 0000 012345678--- [#1(20574)]: kid 1[]'s 2 is my 6 IPC::Run 0000 012345678--- [#1(20574)]: child: `'/bin/./cat'` IPC::Run 0000 012345678--- [#1(20574)]: opening sync pipe IPC::Run 0000 01234567890- [#1(20574)]: pipe() = ( 9, 10 ) IPC::Run 0000 01234567890- [#1(20574)]: fork() = 20575 IPC::Run 0000 0123456789-- [#1(20574)]: close( 10 ) = 0 IPC::Run 0000 01234567890- [#1(20575) cat]: Cleaning up parent's ptty +'0' IPC::Run 0000 0123-567890- [#1(20575) cat]: closing stdin, out, err IPC::Run 0000 ---3-567890- [#1(20575) cat]: open fds: 6 8 4 9 10 5 IPC::Run 0000 ---3-5-7890- [#1(20575) cat]: close( 6 ) = 0 IPC::Run 0000 ---3-5-78-0- [#1(20575) cat]: close( 9 ) = 0 IPC::Run 0000 0--3-5-78-0- [#1(20575) cat]: dup2( 5, 0 ) = 0 IPC::Run 0000 01-3-5-78-0- [#1(20575) cat]: dup2( 5, 1 ) = 1 IPC::Run 0000 0123-5-78-0- [#1(20575) cat]: dup2( 1, 2 ) = 2 IPC::Run 0000 0123-5-78-0- [#1(20575) cat]: dup2( 8, 2 ) = 2 IPC::Run 0000 0123---78-0- [#1(20575) cat]: close( 5 ) = 0 IPC::Run 0000 0123---7--0- [#1(20575) cat]: close( 8 ) = 0 IPC::Run 0000 0123---7--0- [#1(20575) cat]: execing /bin/./cat IPC::Run 0000 0123---7--0- [#1(20575) cat]: exec()ing '/bin/./cat' IPC::Run 0000 0123456789-- [#1(20574)]: read( 9 ) = 0 but true chars ' +' IPC::Run 0000 012345678--- [#1(20574)]: close( 9 ) = 0 IPC::Run 0000 01234-67---- [#1(20574)]: close( 8 ) = 0 Input2pump:1. line IPC::Run 0000 01234-67---- [#1(20574)]: ** pumping IPC::Run 0000 01234-67---- [#1(20574)]: fds for select: ----b-r----- IPC::Run 0000 01234-67---- [#1(20574)]: timeout=forever IPC::Run 0000 01234-67---- [#1(20574)]: selected ----w------- IPC::Run 0000 01234-67---- [#1(20574)]: filtering data to fd 4 (kid's +stdin) IPC::Run 0000 01234-67---- [#1(20574)]: writing to fd 4 (kid's stdin) IPC::Run 0000 01234-67---- [#1(20574)]: write( 4, '1. line' ) = 7 IPC::Run 0000 01234-67---- [#1(20574)]: exiting _select(): io occured +and break_on_io set out: Input2pump:2. line IPC::Run 0000 01234-67---- [#1(20574)]: ** pumping IPC::Run 0000 01234-67---- [#1(20574)]: fds for select: ----b-r----- IPC::Run 0000 01234-67---- [#1(20574)]: timeout=forever IPC::Run 0000 01234-67---- [#1(20574)]: selected ----b------- IPC::Run 0000 01234-67---- [#1(20574)]: filtering data to fd 4 (kid's +stdin) IPC::Run 0000 01234-67---- [#1(20574)]: writing to fd 4 (kid's stdin) IPC::Run 0000 01234-67---- [#1(20574)]: write( 4, '2. line' ) = 7 IPC::Run 0000 01234-67---- [#1(20574)]: filtering data from fd 4 (kid' +s stdout) IPC::Run 0000 01234-67---- [#1(20574)]: reading from fd 4 (kid's stdou +t) IPC::Run 0000 01234-67---- [#1(20574)]: read( 4 ) = 7 chars '1. line' IPC::Run 0000 01234-67---- [#1(20574)]: exiting _select(): io occured +and break_on_io set out:1. line
Here, the output is written but the expected output is:
1. line
2. line
or, the output without debug info:
Input2pump:1. line out: Input2pump:2. line out:
Here, there is nothing in the output
Thank you, Tomas

In reply to IPC::Run and subprocess interaction by xhudik

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.