Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Effect of redirecting output to /dev/null on $? value

by jcb (Vicar)
on Aug 03, 2020 at 00:34 UTC ( #11120246=note: print w/replies, xml ) Need Help??

in reply to Effect of redirecting output to /dev/null on $? value

As ikegami mentioned, the effect on $? is due to running a shell to perform the I/O redirection. If you can assume a Bourne shell, (probably a safe assumption on POSIX systems, which redirection to /dev/null already assumes) then you can use the exec shell command like so:

`exec my_executable_that_seg_faults > /dev/null`;

Note that this is the shell exec inside the qx//, not the Perl exec. The shell exec command causes the shell to replace itself with the desired executable and thus should cause $? to reflect the executable's exit status, instead of the shell's exit status.

Replies are listed 'Best First'.
Re^2: Effect of redirecting output to /dev/null on $? value
by ikegami (Pope) on Aug 03, 2020 at 00:39 UTC

    Backticks always uses the Bourne shell except on Windows. Well, it always uses /bin/sh. While that might not be the Bourne shell, it's going to be something that supports exec.

Re^2: Effect of redirecting output to /dev/null on $? value
by afoken (Canon) on Aug 04, 2020 at 20:27 UTC
    `exec my_executable_that_seg_faults > /dev/null`;

    exec at the start of a command is one of the few things that are treated specially in Perl_do_exec3(), see Re^2: Improve pipe open? (redirect hook). It seems to disable the default optimization and forces the use of the default shell.

    There is a way to stay mentally sane on Unix/POSIX systems for I/O-redirection. Don't use system; instead, fork and exec manually:

    # (untested) my $pid=fork(); my @cmd_and_args=('my_executable_that_seg_faults'); # maybe push @cmd_and_args,qw( some arguments for the program ); defined($pid) or die "Can't fork: $!"; if ($pid) { # parent process waitpid $pid; #<-- sets $? } else { # child process open STDOUT,'>','/dev/null' or die "Can't redirect STDOUT: $!"; exec { $cmd_and_args[0] } @cmd_and_args; die "exec failed: $!"; }

    Note that ONLY indirect object notation on exec prevents all attempts of perl to be smart. See The problem of "the" default shell.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      I have used this before, but our questioner seemed to be asking for a minimal solution. This strategy is the only way if you need more than the basic pipes, but (having just checked) IPC::Open3 is actually implemented using system, presumably for compatibility on Windows, where fork must be emulated.

        open3 only uses system in Windows and OS/2; fork is used elsewhere.

      That's horrible advice.

      The OP is complaining about errors being misreported, and you introduce a new such error.

      exec failures should not appears as exit code from a successfully launched child. Even system and open3 get that right. (They use a close-on-exec pipe to communicate the errno of failures to the parent.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11120246]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-01-25 01:49 GMT
Find Nodes?
    Voting Booth?