1

I have a Perl program and a C program. I want to run the Perl program and capture the return value of C program. To make it clear:

C program (a.out)

int main()
{
    printf("100");
    return 100;
}

Perl program:

print `ls`; #OK
print `a.out`; #No error but it does not print any output.

Any ideas? Thanks.

MvanGeest
  • 9,536
  • 4
  • 41
  • 41
user397232
  • 1,750
  • 6
  • 21
  • 30
  • 5
    Please, for the love of whatever gods you worship, tell us that `a.out` is not your C _source_ file :-) What happens when you run `a.out` from the command line? – paxdiablo Jul 31 '10 at 14:07
  • I bet it should be `./a.out`, since '.' is usually not in PATH. – Borealid Jul 31 '10 at 14:14
  • 'a.out' is the compiled C program and I also tried './a.out'. – user397232 Jul 31 '10 at 14:37
  • @paxdiablo: a.out is a former file format and still the default file name of e.g. GCC when no name for the binary is provided. See http://en.wikipedia.org/wiki/A.out – musiKk Jul 31 '10 at 14:51
  • @musikk, I was actually questioning whether the OP had created an a.out file containing the _source_ code (which, according to comments above, they didn't). – paxdiablo Jul 31 '10 at 14:59
  • @paxdiablo: I see. According to the votes to your comment others think so too. I just found it unreasonable. Seems my reasoning is a bit odd. ;) – musiKk Jul 31 '10 at 16:00
  • Apparently another victim of not using `use warnings`... – Dummy00001 Jul 31 '10 at 16:03

3 Answers3

2

I don't know perl but this works on my system so no guarantees:

#!/usr/bin/perl

print "Running a.out now\n";
$exitCode = system("./a.out");
print "a.out returned:\n";
print $exitCode>>8; print "\n";

For one reason or another system() returns the return value bitshfted by 8 (so 0 will become 256, 1 will be 512... 7 will be 1792 or something like that) but I didn't care to look up why.

Vasiliy Sharapov
  • 997
  • 1
  • 8
  • 27
  • The OP is trying to capture the program's output, not its return value. That's what backticks do. – Ether Jul 31 '10 at 16:26
  • @Ether: I didn't know that, but I think he was just using backticks for brevity, and since his question says "I want to run the Perl program and capture the return value of C program." I'm guessing he just wants the return value. If you have any idea how to capture both and only run it once I would be curious. – Vasiliy Sharapov Jul 31 '10 at 17:00
  • 1
    See http://stackoverflow.com/questions/109124/how-do-you-capture-stderr-stdout-and-the-exit-code-all-at-once-in-perl – daotoad Jul 31 '10 at 17:19
  • @Ether the question clearly says the return value, so system is the right way to go – thecoshman Dec 20 '12 at 15:35
1

Your C program is not printing a carriage return, so you may be seeing line buffering issues.

Try this instead:

printf("100\n");
Ether
  • 53,118
  • 13
  • 86
  • 159
0

system() will return a code indicating what your C program returned or if it was terminated by a signal; assuming the later is not the case, you can do

$exitcode = system('a.out');
print "return code was ", $exitcode >> 8, "\n";

If you also wish to capture the output, you can use backticks and the code will be in the $? variable.

$output = `a.out`;
$exitcode = $?;
print "return code was ", $exitcode >> 8, "\n";
print "output was:\n", $output;

You may want to use a module like IPC::Cmd that has several other features you may want.

ysth
  • 96,171
  • 6
  • 121
  • 214