I have a Perl program(this is the PARENT) which needs variables from another Perl prog(CHILD). I want to realize that with the pipe mechanism of Linux. After unsuccessfull search in net (none of the numerous examples fit this basic theme -- I think the concept is quite understandable for a nonexpert, but I cannot implement a running example). The two appended progs show my understanding of piping, which is probably totally wrong, but I want to learn it. For clarity:
Prog PARENT is running
needs 2 variables from Prog CHILD
PARENT calls CHILD (open CHILD ... ?)
CHILD is running and can deliver the 2 variables
CHILD opens PARENT, write/print the variables to PARENT
CHILD closes PARENT
CHILD exit
PARENT can now read from CHILD
The PARENT Program (Caller and Receiver)
#!/usr/bin/env perl
use strict;
use warnings;
# file-name: mwe-ipc.pl
# this prog. is the PARENT
# Calls a CHILD by its prog.name
print "$0\n"; # show your progname
my $pid = open(CHILD, "mwe-ipc-child.pl |") or die "Couldn't fork: $!\n";
my @arr_receiver;
while (<CHILD>){
# PARENT needs two variables from CHILD
# how to get var1 and var2?
@arr_receiver = $_;
}
close(CHILD);
print "arr_receiver[$_] = $arr_receiver[$_]\n" for (0..$#arr_receiver);
The CHILD Program (will be called and answers)
#!/usr/bin/env perl
use strict;
use warnings;
# file-name: mwe-ipc-child.pl
# this prog. is the CHILD
# Called from a PARENT
print "$0\n";;
my $pid = open(PARENT, "| mwe-ipc.pl") or die "Couldn't fork: $!\n";
my $var1 = "a"; #"|l |p{2.7cm} |p{2cm}";
my $var2 = "b"; #"\textbf{G}& \textbf{Substantiv}& \textbf{Modus} \\";
while (<PARENT>){
# PARENT needs two variables from CHILD
# how to put var1 and var2?
print PARENT $var1, $var2;
}
close(PARENT);
exit(0);
Call to PARENT Prog outputs progname, then endless loop. Ultimately needless call from cmdline of CHILD delivers own and PARENTS' progname. Can someone please help?