Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Best method to capture return code from system calls?

by Anonymous Monk
on Mar 30, 2011 at 07:58 UTC ( [id://896336] : note . print w/replies, xml ) Need Help??


in reply to Best method to capture return code from system calls?

The return code from "system" is not unreliable, you're just interpreting it backwards. It's a really easy mistake to make - the shell interprets 0 as success and nonzero as failure. So a zero return code from a system call means it succeeded. The actual return code, if nonzero, is returned in the high-order byte of the return code, so if the shell returns 1 you'll get 256. That is usually not what you care about, you just want to know did it succeed or fail. So - change "or" to "and" and you're all set. It is clearer to say: if(system(...) != 0) { die(" ... "); }
  • Comment on Re: Best method to capture return code from system calls?