http://www.perlmonks.org?node_id=1058246

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi, I know there is a lot of lit around this. But i am not able to get this clear. My parent perl program start another child script. The child script reads data from <STDIN>. My question, how to i pass on this data from parent to child? Thanks RJ
  • Comment on Child process reading <STDIN> from parent

Replies are listed 'Best First'.
Re: Child process reading <STDIN> from parent
by Laurent_R (Canon) on Oct 15, 2013 at 07:15 UTC

    My question, how to i pass on this data from parent to child?

    You mean from child to parent, don't you? Otherwise it does not make sense to me.

      My parent program launches the child like below:

      parent.pl

      =========

      ..... launch_chld{ my $cmd = "val_report.pl killall"; my $h = IO::Handle->new; die "IO::Handle->new failed." unless defined $h; open $h, $cmd . ' 2>&1 |'; $h->autoflush(1); }
      ********************************************

      val_report.pl

      =========

      .... ... process_args{ if(XYZ){ print "Do you really want XYZ. <YES> or <NO>"; die "Cant proceed as confirmation is NO" if(<> ne 'YES'); } }
      ****************************************

      Now process args will expect a input from user which in this case is parent.pl.

      The question is how do i pass a "YES" or "NO" to process_args?

        If I understand correctly the problem is not just that you're trying to read from STDOUT or write in STDIN of another process, it's that you're trying to do both at once.

        perlopentut still gives you the answer on that one: it redirects to the perlipc documentation on bidirectionnal communication with another process. This seems quite simple, all you have to think about is disable buffering on both sides.

        Just to add the $cmd also does o/p to stdout which i want to process which i capture via Tk::fileevent

Re: Child process reading <STDIN> from parent
by Eily (Monsignor) on Oct 15, 2013 at 07:59 UTC

    Why do you want to do that in the first place? Not that there aren't perfectly good reason to try and write on another process' STDIN, but since you don't seem to know very well what you are doing, I can't help but wonder if you didn't take that problem by the wrong end.

    Then, to answer your question, from what little information you gave us, it looks like a job for pipes. In perlopentut you'll see how to open a process and obtain a filehandle to read from its output or write in its input.

      Sorry, i posted the original question without logging in. I am a new user.

      I have provided details as rjohn1(my id)

      Hope somebody could look into the code

        I would recommend you read Eily's post. I think that will get you on the best path.
Re: Child process reading <STDIN> from parent (open)
by tye (Sage) on Oct 15, 2013 at 15:08 UTC
    open my $pipe, "| $script" or die "Can't fork to run $script: $!\n"; print $pipe $input; close $pipe

    - tye        

      Thanks tye, this is what i wanted. Though i tried something on the same lines before posting, but this simply worked.