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

看板Perl作者 (Cindy Wang)時間8年前 (2017/03/16 18:10), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《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. 你想要的資訊應該是在我標黃色的部分~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 112.121.78.5 ※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1489659045.A.CD5.html

03/17 19:51, , 1F
感謝大大分享
03/17 19:51, 1F
文章代碼(AID): #1OocIbpL (Perl)
討論串 (同標題文章)
文章代碼(AID): #1OocIbpL (Perl)