Can a Perl system() call ever die?

戏子无情 提交于 2019-12-12 10:36:27

问题


Can a system() call can ever die in Perl 5?

(in other words, in order to 100% crash-proof a program that does a system() call, does it need to be wrapped into an eval block, or is that wholly totally unnecessary?)


I haven't found a single mention to that possibility in perldoc system, but didn't quite find the precise "this call never dies" either.

NOTE: the question is about basic CORE Perl here, no autodie or any other custom module that would have similar effect. Also, assume no ALRM signal was set, or any other custom signal handler for that matter.

I'm assuming that all versions of Perl 5.* behave the same, but if not, an answer pertaining to 5.8 would be appreciated.


回答1:


Unless my interpretation of the source is incorrect, this looks like a possibility:

Source: Perl 5.16.2 (checked 5.8.8 too), file: pp_sys.c, line: 4224 within the PP(pp_system) code block:

if (n != sizeof(int))
  DIE(aTHX_ "panic: kid popen errno read, n=%u", n);

DIE is Perl_die(pTHX_ const* pat, ...) declared in util.c

According to the documentation, "panic: kid popen errno read" means "forked child returned an incomprehensible message about its errno".

Explanation of panic messages in Perl:

The convention is that when the interpreter dies with an internal error, the message starts "panic: ". Historically, many panic messages had been terse fixed strings, which means that the out-of-range values that triggered the panic are lost. Now we try to report these values, as such panics may not be repeatable, and the original error message may be the only diagnostic we get when we try to find the cause.




回答2:


You can call system() with the expectation that it will not throw an exception. There is no need to wrap it in an eval block.




回答3:


system returns the exit status of the program. It means, if the program crashes, the calling Perl script continues (see system).

Nevertheless, the program itself can still kill the calling script or even crash the computer. For example, in Linux:

system 'killall', 'perl';
print "Alive\n";



回答4:


I assume that you're talking about the implementation of the system function itself as opposed to whatever is invoked via the call. (Obviously, a child process can't call die in the parent's context, and even that assumes that the call is to Perl code.)

A definitive answer would require knowledge of the internals but given that attempting to invoke a non-existent program doesn't die, I can't imagine that anything else ever would, either:

system('abcd');      # 'abcd' is not recognized... [Win32 message]
say "I'm not dead."; # always prints


来源:https://stackoverflow.com/questions/13234866/can-a-perl-system-call-ever-die

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!