Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Why should I use this? Is there an alternative way?
I am calling some binary in
system(binary)
which I created in C++, which does some stuff and if it fails it gives
assert
. When I reboot a Linux machine, the stuff which is going fails, and my binary generates
assert
as expected. But on the Perl side where I called it, it throws a
134
error code, which after
134 >> 8
, becomes
0
. Ultimately it is making my failure operation a
success
(which I don't want).
–
–
The return value is the exit status of the program
as returned by the
wait
call. To get the actual
exit value divide by 256.
You can check all the failure possibilities by
inspecting
$?
like this:
$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;
–
The program executed under system
may return a specific code when it exits. That is packaged into high bits of a number (exit status) which Perl gets and which is available in $?
. This is why you want to test that number, for example (but see below)
system($cmd) == 0 or die "Error: $?";
Or, you can separately check $? after the call. If true (non-zero), that only means that there was something other than a clean exit by the application, or a problem with system
itself. When you unpack that you are looking for what the application communicated on its exit. In order to merely see whether there was an error you only look at $?
, and you are getting a value. So there is no conflict.
What code that program returned is its business (design decision). Programs ideally exit with codes when they fail (if they can detect and handle the problem), and have documentation explaining what code means what. For an example see this answer, and for another comment this one.
As seen in system, you readily get: exit code $? >> 8
, signal number $? & 127
, and whether core was dumped $? & 128
. With your exit status of 134
the signal number is 6
, which man 7 signal
lists as SIGABRT
. The core should be there as well. So that is what you got from your program, and no explicit exit code. Apparently, the program caused abort and dumped core.
In your case, you know where all this comes from -- assert
is a macro which calls abort
, whereby SIGABRT
is raised (man 3 assert abort
). Perl gets back that 6
packaged into 134
.
Note that assert
prints a message to STDERR
, so you may want to run the program via qx
(backticks), in which case you can capture that error. See the first link above, or search on SO.
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.