in reply to Redirecting STDOUT

I don't use IO::File, but this is a good use of local:

{ local *STDOUT; open( STDOUT, '>/dev/null' ) or die "Cannot redirect STDOUT: $!\n" +; $rc = system( @command ); }

Replies are listed 'Best First'.
Re: Re: Redirecting STDOUT
by chip (Curate) on May 02, 2003 at 19:37 UTC
    I believe that won't work, based on some tests. Localizing *STDOUT breaks the magic that ensures that ensures that the STDOUT handle always has fd 1, and the fd is the only thing that matters when spawning a child. Something similar that should work is:
    { local *FOO; open FOO, ">&STDOUT"; # fileno(FOO) = dup(1) open STDOUT, ">/dev/null"; # dup2(open(), 1) system @command; open STDOUT, ">&FOO"; # dup2(fileno(FOO), 1) }

        -- Chip Salzenberg, Free-Floating Agent of Chaos