【发布时间】:2019-11-15 17:32:51
【问题描述】:
我有一个 Perl 程序(这是父程序),它需要来自另一个 Perl 程序(子)的变量。我想通过 Linux 的管道机制来实现这一点。在网上搜索不成功后(众多示例中没有一个符合这个基本主题——我认为这个概念对于非专家来说是完全可以理解的,但我无法实现一个运行示例)。两个附加的程序显示了我对管道的理解,这可能是完全错误的,但我想学习它。为清楚起见:
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
家长计划(呼叫者和接收者)
#!/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);
儿童计划(将被调用并回答)
#!/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);
调用 PARENT Prog 输出 progname,然后是无限循环。 最终,来自 CHILD cmdline 的不必要调用提供了自己和父母的 progname。 有人可以帮忙吗?
【问题讨论】:
标签: perl