Perl 和外部程序
我有一个 Perl 程序和一个 C 程序。我想运行Perl程序并捕获C程序的返回值。为了说清楚:
C 程序(a.out)
int main()
{
printf("100");
return 100;
}
Perl 程序:
print `ls`; #OK
print `a.out`; #No error but it does not print any output.
有什么想法吗? 谢谢。
I have a Perl program and a C program. I want to run the Perl program and capture the return value of C program. To make it clear:
C program (a.out)
int main()
{
printf("100");
return 100;
}
Perl program:
print `ls`; #OK
print `a.out`; #No error but it does not print any output.
Any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不了解 perl,但这适用于我的系统,所以不能保证:
出于某种原因,
system()
返回返回值按 8 位移动(因此 0 将变为 256,1 将变为 512。 .. 7 将是 1792 或类似的东西)但我不想查为什么。I don't know perl but this works on my system so no guarantees:
For one reason or another
system()
returns the return value bitshfted by 8 (so 0 will become 256, 1 will be 512... 7 will be 1792 or something like that) but I didn't care to look up why.您的 C 程序没有打印回车符,因此您可能会遇到行缓冲问题。
试试这个:
Your C program is not printing a carriage return, so you may be seeing line buffering issues.
Try this instead:
system() 将返回一个代码,指示您的 C 程序返回了什么或者它是否被信号终止;假设后者不是这种情况,你可以做
如果你也希望捕获输出,你可以使用反引号,代码将在 $?多变的。
您可能想要使用像 IPC::Cmd 这样的模块,它具有您可能需要的其他几个功能。
system() will return a code indicating what your C program returned or if it was terminated by a signal; assuming the later is not the case, you can do
If you also wish to capture the output, you can use backticks and the code will be in the $? variable.
You may want to use a module like IPC::Cmd that has several other features you may want.