Your Mother has asked for the wisdom of the Perl Monks concerning the following question:

When doing unit/exception tests (especially in stuff like Moose that's stacktrace heavy) STDERR can get ugly and cloud the (terminal/human) results making PASSes unpleasant to look at and making it hard to find the germane bits in FAILs. So, this-

close STDERR or warn "Couldn't close STDERR"; open STDERR, ">", "/dev/null" or exit 1; # Nope: die "Can't open STDER +R to /dev/null: $!";

Is that best, most sane way to do it? Is there a CPAN alternative to avoid cookie cutter propagation of the snippet? WWJAPHD?

Update: noticed the problem with dieing after closing STDERR. Der...

Replies are listed 'Best First'.
Re: Silencing STDERR for tests
by roboticus (Chancellor) on Dec 30, 2010 at 18:14 UTC

    Yer Mum:

    Maybe you should modify the calling test framework to do the redirect, and let it execute your script. That would eliminate "cookie-cutter" code. (If I were to do it, I'd log it to a file so you could read it in the event that some tests fail.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Silencing STDERR for tests
by afoken (Chancellor) on Dec 31, 2010 at 10:03 UTC

    Raw ideas:

    • $SIG{__WARN__}=sub { warn substr($_[0],0,100) }; $SIG{__DIE__}=sub { die substr($_[0],0,100) # unless in eval ... };
    • tie *STDERR,'Test::JustShutup'; # Test::JustShutup to be written ... ;-)
    • IO::Capture::Stderr (using a tie behind the scenes)
    • my $null=File::Spec->devnull(); open STDERR,'>',$null or die "Could not open '$null': $!"; # no close() before open(), open() takes care of closing STDERR at the + right time

    Alexander

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