Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to turn off STDOUT portably without closing it?

Reply
Thread Tools

How to turn off STDOUT portably without closing it?

 
 
kj
Guest
Posts: n/a
 
      05-05-2006



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

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-05-2006
kj wrote:
> 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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-05-2006

kj wrote:

> 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.

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      05-05-2006

Paul Lalli wrote:
> 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?

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-05-2006

Brian McCauley wrote:
> Paul Lalli wrote:
> > 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?


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Viewstate wont turn off even after setting it to off ! robert112 ASP .Net 1 04-26-2007 01:51 AM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57