CC: | dev [...] perlcritic.tigris.org |
Subject: | Strange side effects on STDERR |
I noticed that the latest version of Perl::Tidy requires the stderr
parameter to be an actual filehandle, not just a scalar reference. So
if I want to capture stderr into a scalar, I must use an IO::String
object or something similar.
The problem is that Perl::Tidy assigns that object directly to *STDERR.
And when that object goes out of scope, it closes STDERR. So
subsequent calls to warn() fail because the output handle is now gone.
The attached file demonstrates this.
I think the solution might be for Perl::Tidy to use a lexical (or
localized) filehandle rather than STDERR directly.
Thanks for your consideration.
--
Jeffrey Thalhammer
Imaginative Software Systems
www.imaginative-software.com
Subject: | test.pl |
#!perl
use strict;
use warnings;
use IO::String;
use Perl::Tidy 20120701;
my $tidy_src = '';
my $raw_src = q{ print ('Hello World') ; };
{
my $stderr_buffer = '';
my $stderr_fh = IO::String->new( \$stderr_buffer );
local @ARGV = qw(-nst -nse);
Perl::Tidy::perltidy( destination => \$tidy_src,
source => \$raw_src,
stderr => $stderr_fh );
}
# This next line throws a fatal exception because Perl::Tidy has tied
# STDERR to our $stderr_fh (which is an IO::String object), and when
# that object is destroyed, it closes the handle (thus closing
# STDERR). So calling warn() will fail because the output handle
# isn't there any more. Strangely, calling CORE::warn still works.
warn "Tidied code is $tidy_src";