http://www.perlmonks.org?node_id=291332


in reply to Re: Best Way to Redirect STDERR to a Scalar
in thread Best Way to Redirect STDERR to a Scalar

Okey-dokey, I can understand frustration with packages that don't allow you to (easily) capture their debugging output.   Please try the following which works on ActiveState 5.6.1 build 635 running on Windows XP.   Amazing, no open's, no close's, but all TIE'd up prettily anyway.
my $data; print STDERR " STDERR test (1)\n"; tie *STDERR, 'IO::Scalar', \$data; print STDERR " STDERR test (2)\n"; untie *STDERR; print STDERR " STDERR test (3)\n"; printf " Scalar string has length %d\n", length($data); printf " and contains '%s'\n", $data;
Outputs
 STDERR test (1)
 STDERR test (3)
  Scalar string has length 17
    and contains ' STDERR test (2)
'

Update: Drat, it works wonderfully until you hit submit, then you think of the case that doesn't work.   If I throw in a warn which presumably uses the filehandle more directly:

tie *STDERR, 'IO::Scalar', \$data; print STDERR " STDERR test (2)\n"; warn " STDERR test (2a)"; untie *STDERR;
I then see
 STDERR test (1)
 STDERR test (2a) at mrmskrat3.pl line 14.
 STDERR test (3)
  Scalar string has length 17
    and contains ' STDERR test (2)
'
showing that some output to 'STDERR' isn't going to be captured this way.   So it will very much depend on the way the package outputs to STDERR whether this will work.   Rats!