How to turn off STDOUT portably without closing it?

Discussion in 'Perl Misc' started by kj, May 5, 2006.

  1. kj

    kj Guest

    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.
     
    kj, May 5, 2006
    #1
    1. Advertising

  2. kj

    Paul Lalli Guest

    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
     
    Paul Lalli, May 5, 2006
    #2
    1. Advertising

  3. 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.
     
    Brian McCauley, May 5, 2006
    #3
  4. 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?
     
    Brian McCauley, May 5, 2006
    #4
  5. kj

    Paul Lalli Guest

    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
     
    Paul Lalli, May 5, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    470
    Cheung, Jeffrey Jing-Yen
    Jul 8, 2003
  2. Andy Crawford

    How to turn Strict "off"?

    Andy Crawford, Jul 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    533
    Cowboy \(Gregory A. Beamer\)
    Jul 11, 2003
  3. Andrew Crawford

    Re: How to turn Strict "off"?

    Andrew Crawford, Jul 12, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    376
    Kevin Spencer
    Jul 13, 2003
  4. robert112
    Replies:
    1
    Views:
    460
    Juan T. Llibre
    Apr 26, 2007
  5. thomas
    Replies:
    0
    Views:
    236
    thomas
    Oct 23, 2003
Loading...

Share This Page