perl, c program returns a negative value

Posted on 2012-09-12
Last Modified: 2012-09-28
I have a perl program that calls a c program.  When I print $return_val, I don't get the same number that the my_c_program returned.  my_c_program returns negative and positive number.

my $return_val = '/tmp/my_c_program';
Question by:rgbcof
    LVL 84

    Expert Comment

    my $return_val = '/tmp/my_c_program';
    does not call a c program, it assigns a literal string to $return_val

    If you had
    my $return_val = `/tmp/my_c_program`;
    then $return_val would contain whatever was printed to STDOUT by the execution of /tmp/my_c_program
    LVL 13

    Accepted Solution

    If you are looking for the actual return code, you need to use the special $? variable.
    my $StandardOutput =`/path/to/your/command with options`;
    my $ReturnCode = $? >> 8;

    Open in new window

    Note that you need to bit-shift the return code as I did to get the "normal" return code that you might get via a shell script (for example).  The extra bits are flags that include information such as whether a core file was generated or not.  In most cases, you don't need those bits if you just want to see whether the command returned zero or not.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now