I am writing several tests using Test::More , and one of the functions that I am testing is printing to STDERR . I would like to test the result on STDERR , but a little unsure how to do it. I know that I'm near. It works:
use strict; use warnings; use feature qw(say); close STDERR; open STDERR, ">", \my $error_string; say STDERR "This is my message"; say qq(The \$error_string is equal to "$error_string");
This produces:
The $error_string is equal to "This is my message "
However, I do not want to close STDERR. I just want to trick him.
I tried this:
use strict; use warnings; use feature qw(say); open my $error_fh, ">", my $error_string; open STDERR, ">&", $error_fh; say STDERR "This is my message"; close $error_fh; say qq(The \$error_string is equal to "$error_string");
But, $error_string empty.
What am I doing wrong?
source share