How to turn off STDOUT portably without closing it?

K

kj

ExtUtils::Install::install is making some unwanted noises ("Installing
blah-blah-blah", via STDOUT) that I want to turn off, and do so in
a portable way. My first stab was simply to close STDOUT:

perl -MExtUtils::Install -e '
close STDOUT; install( { "foo" => "bar" }, 0, 0, 0 )'

....but this results in *new* unwanted noises:

Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.
Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.
Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.

....etc., on such line for each file copied.

I tried adding 'no warnings "io";' to the script, but that failed
to silence the warnings (why?). The only portable solution I could
find is very long-winded:

perl -MExtUtils::Install -e '
$SIG{ __WARN__ } = sub{ warn @_ unless $_[ 0 ] =~ /STDOUT reopened/ };
close STDOUT;
install( { "foo" => "bar" }, 0, 0, 0 )'

A shorter solution is

perl -MExtUtils::Install -e '
open STDOUT, ">/dev/null";
install( { "foo" => "bar" }, 0, 0, 0 )'

....but I doubt that it is portable.

A third solution would be

perl -MExtUtils::Install -e '
close STDOUT;
open STDOUT, ">", \$toss;
install( { "foo" => "bar" }, 0, 0, 0 )'

but I believe this would bomb with versions of Perl older than 5.8,
so in a sense it is not very portable either.

Is there a more succinct solution than my __WARN__ hack above?

Thanks!

kj
 
P

Paul Lalli

kj said:
ExtUtils::Install::install is making some unwanted noises ("Installing
blah-blah-blah", via STDOUT)

That is a little bizarre. Looking at the source for the module, it
seems like there's a few print statements that are not appended with
"if $verbose...". It almost seems like there should be a new verbose
level, like -1, that makes it print nothing at all...
that I want to turn off, and do so in
a portable way. My first stab was simply to close STDOUT:

perl -MExtUtils::Install -e '
close STDOUT; install( { "foo" => "bar" }, 0, 0, 0 )'

...but this results in *new* unwanted noises:

Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.
Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.
Filehandle STDOUT reopened as FH only for input at /usr/lib/perl5/5.8.6/File/Copy.pm line 118.

I can't reproduce this, at least not with the one liner you give above.
...etc., on such line for each file copied.

I tried adding 'no warnings "io";' to the script, but that failed
to silence the warnings (why?).

use warnings and no warnings are lexical. You set them in your script,
but the warning was originating from the File::Copy module. You'd have
to go into that module and turn off the warning there. Or, you could
try the "dear-god-please-don't-ever-use-this" -X command line
option....
A shorter solution is

perl -MExtUtils::Install -e '
open STDOUT, ">/dev/null";
install( { "foo" => "bar" }, 0, 0, 0 )'

...but I doubt that it is portable.

<shrug> Opening STDOUT to /dev/null doesn't give me any errors or
warnings on my Win32 box, and successfully prevents prints from being
displayed...

Paul Lalli
 
B

Brian McCauley

kj said:
open STDOUT, ">/dev/null";

...but I doubt that it is portable.

require File::Spec;
open STDOUT, '>', File::Spec->devnull;

I can't recall how long File::Spec has been CORE.

You may want to chnage the second comma to a period.
 
B

Brian McCauley

Paul said:
Opening STDOUT to /dev/null doesn't give me any errors or
warnings on my Win32 box, and successfully prevents prints from being
displayed...

This is a really scary bit of OTT DWIM. I think it's a step too far.

Is it even documented?
 
P

Paul Lalli

Brian said:
This is a really scary bit of OTT DWIM. I think it's a step too far.

Is it even documented?

No idea. I just stated that it "worked", not that it was a good idea.
Your File::Spec suggestion else-thread is clearly the better option.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top