0

如果我运行一个 perl 程序并使用反引号调用另一个 perl 程序,则来自被调用程序的打印语句不会出现在终端上。

如果我使用“系统”调用程序,则会显示打印语句。

例如:这是 ProgA.pl

print "In ProgA.pl, about to call ProgB.pl";
my $dum=`ProgB.pl`;                       # print output doesn't appear
### $dum=system("ProgB.pl");              # this prints OK
print"\nBack in ProgA.pl";
print "\ndum = $dum";             # ProgB's output doesn't show here either

(没有警告或错误,通过文件关联找到perl.exe)

这是 ProgB.pl:

print "\nPrinting from ProgB.pl";

差异的原因是什么?

为什么 $dum 中没有返回反引号调用输出(我尝试了 STDOUT 和 STDERR)?如果我在反引号中调用 dir,我会在 $dum 中得到它的输出。

4

2 回答 2

2

你有一个路径问题。

如果我将反引号从 更改为,它会按预期工作($dum分配值“ ”) 。如果没有显式路径,它会搜索系统路径并生成错误,您可以看到是否将该行更改为Printing from ProgB.plProgB.pl./ProgB.pl./

my $dum=`ProgB.pl` or die $!;

生成输出

In ProgA.pl, about to call ProgB.plNo such file or directory at ./ProgA.pl line 4.

从而再次说明您应该始终检查系统调用的返回值是否存在错误情况。

于 2018-01-18T10:47:15.650 回答
1

看来,由于未能在 ProgB 中的打印命令末尾放置换行符,我未能在返回 ProgA 之前刷新缓冲区。感谢克里斯·特纳。

于 2018-01-18T18:59:49.070 回答