我所指的程序是在本节这里中显示的第二个程序。它的一个小改动是:
#!/usr/bin/perl -w
use IPC::Open2;
use Symbol;
$WTR = gensym(); # get a reference to a typeglob
$RDR = gensym(); # and another one
$pid = open2($RDR, $WTR, 'bc');
print "$pid\n";
while (<STDIN>) { # read commands from user
print $WTR $_; # write a command to bc(1)
$line = <$RDR>; # read the output of bc(1)
print STDOUT "$line"; # send the output to the user
}这个程序运行正常。如果它的名称是prop_7_2_39_2.pl,那么与它的典型交互是:
>./prop_7_2_39_2.pl
75955
2+2
4
quit
>也就是说,在输入“退出”之后,子进程bc就失效了,之后我需要输入一个换行符才能真正完成perl父进程。为什么<STDIN>被评估为false?我理解perl计算<STDIN>的定义。一些相关的程序
#!/usr/bin/perl -w
while(<STDIN>){}并没有结束。
发布于 2020-08-04 07:01:07
在将quit发送到bc之后,它将终止,从而关闭管道的读取端。然后,您的下一个print $WTR $_将失败并生成终止程序的SIGPIPE信号--除非您为它安装了信号处理程序。
另一种解决方案可能是在您向bc发送了一些信息之后检查它的读取结果:
while (<STDIN>) { # read commands from user
print $WTR $_; # write a command to bc(1)
my $line = <$RDR>; # read the output of bc(1)
if($line) {
print STDOUT "$line"; # send the output to the user
} else {
last; # break out of the while loop
}
}
print "Controlled ending...\n";https://stackoverflow.com/questions/63240983
复制相似问题