Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I am bit new to perl. I have C program whose main function returns an int,
How do you read that in perl(infact mod perl).Please let me know. Also what does "!$?" signifies.

Thanks in Advance

Replies are listed 'Best First'.
Re: Reading return value from C program
by jdporter (Paladin) on Mar 15, 2007 at 17:59 UTC

    On most operating systems like Windows and UNIX, one program can call (execute) another. When the called program ends, it returns to the calling program a status code, which is an integer. (Normally, a code of 0 indicates success.)

    If the called program is written in C, it returns this status code via the return value of the main() function. Other languages do it differently. In Perl, you can do it by giving an argument to the exit function.

    When the calling program is written in Perl, and the called program is executed using the system function, the return value of that function is (or rather, contains) the status code returned by the called program. So you can do

    my $child_code = system( "other_program" );
    There are other ways to execute another program (such as the qx operator), and they don't return the child's status code quite so conveniently, so in those cases you look for it in the special $? variable. E.g.:

    my $result_text = qx( other_program ); my $child_code = $?;
    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: Reading return value from C program
by ikegami (Patriarch) on Mar 15, 2007 at 17:57 UTC
    If you launched the program using system, the child's exit code is returned by system. It is also placed in $?. See the example in the docs on how to do error handling and on how to extract the exit code.

    If the child was launched asynchronously, waitpid will place the exit code in $?.

Re: Reading return value from C program
by Anno (Deacon) on Mar 15, 2007 at 17:56 UTC
    How does your C program return an integer? Does it print it to the screen? Return it as its exit value? Something else? The answer to your question depends entirely on that.

    As a Perl expression, !$? is the logical negation of the value of the system variable $?. If you are in fact after the exit value of a program, this (the variable, not its negation) would be the place to look, but read about $? in perldoc perlvar.

    Anno

      I got my answer. We can use "$?>>8" to get the read the return value, however it's returning only positive value.Any one knows how to read the negative values as well
        You can interpret the byte as a signed byte:
        my $exit_code = $? >> 8; $exit_code -= 256 if $exit_code > 127;
        But that won't tell you whether the program has specified its exit code as a negative number. All you get is the least significant eight bytes of the value.

        Anno

Re: Reading return value from C program
by agianni (Hermit) on Mar 15, 2007 at 17:57 UTC

    You might find this thread insightful.

    As for !$?, $? is a Perl special variable for child error. So if your script forks a child process and there is an error, this will contain that error. ! is the not operator. I'm guessing you're seeing something like:

    if ( !$? ){ # do the stuff you do after a forked process completes sucessfully }

    But that would be much easier to read as:

    use English qw(no_match_vars); if ( not $CHILD_ERROR ){ # do the stuff you do after a forked process completes sucessfully }

    Updated comments to reflect meaning of $?

      fork sets $!, not $?.
        agianni's code wouldn't make sense after a fork(), that's true. It would make sense after a waitpid. Maybe that was the tacit assumption. It would have been better made explicit.

        Anno