in reply to Running a C program from Perl

Just to point out something odd about this line in the OP code:
$result = `$bin > output.txt`;
The backticks around the command-line string tell perl to capture whatever output is produced on the command's stdout, but since the command includes a redirection of stdout to a file, there will be nothing available for perl to assign to your "$result" variable -- it will get an empty string, because all the stdout content from "hello.txt" is written (via redirection) to the "output.txt" file.

I get the impression that this isn't what you intended. If you want $result to store the (numeric) exit status of the command, use system; if you want it to get the stdout content from the command, remove the  > output.txt part from the command line string.

Replies are listed 'Best First'.
Re^2: Running a C program from Perl
by n4nature (Novice) on May 26, 2009 at 05:36 UTC
    I was wondering why $result was empty. Thanks! If I want to read and analyze results in Perl from a file (output.txt generated by C++ program), what's the command/module for that? Thanks.
Re^2: Running a C program from Perl
by Bloodnok (Vicar) on May 26, 2009 at 16:06 UTC
    By way of pedantry, when you say ...nothing available for perl to assign..., isn't quite right since $result can be non-empty i.e. iff $bin generates anything to STDERR ... either by accident or design :D

    A user level that continues to overstate my experience :-))
      $result can be non-empty i.e. iff $bin generates anything to STDERR .

      If the command in backticks generates any output on its own stderr, that goes directly to the perl script's STDERR, and does not get captured/assigned by the backticks -- in the OP case, $result will still be empty, because backticks only return what went to the command's stdout.

      Now, if the command being run in backticks included redirection of stderr to stdout (and no subsequent redirection of stdout to some file), then backticks would capture any stderr content along with any stdout -- e.g. if your backticks are executing a bourne-style shell, the following would generally include some sort of error message to the perl variable, along with any matching lines found by grep in ~/.bashrc:

      my $result = `grep PATH ~/.bashrc /no/such/file 2>&1`
        Hmmm,

        I think it looks like I and to some extent you, are barking up the wrong tree (well on linux & AIX anyway) - consider the following behaviour exhibited by both Linux 2.6.28-11-generic and AIX 5.3:

        user@unforgiven:~$ egrepa bash: egrepa: command not found user@unforgiven:~$ egrepa >/dev/null bash: egrepa: command not found user@unforgiven:~$ egrepa >/dev/null 2>&1 user@unforgiven:~$ perl -e '$res = `egrepa 2>&1`; print $res' user@unforgiven:~$
        Personally, I would expect the one-liner to print bash: egrepa: command not found - as it does if entered from command line - but doesn't.

        If, however, as you suggest, a valid command is used with invalid opts, then the re-direction works as expected and the error is seen (on both platforms).

        A user level that continues to overstate my experience :-))