in reply to Re: Running a C program from Perl
in thread Running a C program from Perl

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 :-))

Replies are listed 'Best First'.
Re^3: Running a C program from Perl
by graff (Chancellor) on May 27, 2009 at 02:16 UTC
    $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 :-))

        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.

        The shell was never invoked, so how can it display an error message? It's up to you to do your own error checking.

        $ perl -e'$res = `egrepa 2>&1`; print "$?:$!\n"; print $res' -1:No such file or directory (( Perl tried to execute egrepa, couldn't, and returned an error. Perl doesn't output error messages for you. )) $ perl -e'$res = `egrepa "" 2>&1`; print "$?:$!\n"; print $res' 32512: sh: egrepa: command not found (( Perl sees a shell command, and successfully executed the shell. The shell couldn't execute egrapa "", so it displayed and returned an error. ))