Re: [問題] 程式返回值疑問

作者: CindyLinz (Cindy Wang)   2017-03-16 18:10:41
※ 引述《clanguage (C語言)》之銘言:
: 請問一下從 perl 呼叫其他 script 程式返回值問題
: 1.pl 呼叫 2.pl 後, 我傳回值為1, 但在 1.pl 卻收到 256
: 有人知道是怎麼回事嗎?
: 1.pl
: my $cmd = "2.pl";
: system($cmd);
: if ($? > 0) {
: B say "result=$?";
: }
: # 印出 result=256
: 2.pl
: my $ret = main();
: say "main=$ret";
: exit($ret);
: # 印出 main=1
這邊 http://perldoc.perl.org/perlvar.html#Error-Variables
你也可以 man perlvar 來看
$? 的段落是這樣寫的:
$?
The status returned by the last pipe close, backtick (`` ) command,
successful call to wait() or waitpid(), or from the system() operator.
This is just the 16-bit status word returned by the traditional Unix wait()
system call (or else is made up to look like it). Thus, the exit value of
the subprocess is really ($?>> 8 ), and $? & 127 gives which signal, if any,
the process died from, and $? & 128 reports whether there was a core dump.
Additionally, if the h_errno variable is supported in C, its value is
returned via $? if any gethost*() function fails.
If you have installed a signal handler for SIGCHLD , the value of $?
will usually be wrong outside that handler.
Inside an END subroutine $? contains the value that is going to be
given to exit(). You can modify $? in an END subroutine to change
the exit status of your program. For example:
END {
$? = 1 if $? == 255; # die would make it 255
}
Under VMS, the pragma use vmsish 'status' makes $? reflect the actual
VMS exit status, instead of the default emulation of POSIX status;
see $? in perlvms for details.
Mnemonic: similar to sh and ksh.
你想要的資訊應該是在我標黃色的部分~
作者: clanguage (C語言)   2017-03-17 19:51:00
感謝大大分享

Links booklink

Contact Us: admin [ a t ] ucptt.com