Subject: | Exception for special filehandles in InputOutput::RequireBracedFileHandleWithPrint |
After some debate, we've decided in my company that braces around the
builtin filehandles is obnoxious, so I customized
RequireBracedFileHandleWithPrint for ourselves locally and thought I
contribute back in case someone thinks this would work in the core
distro (even though it no longer conforms to PBP exactly). I just added
a simple boolean switch so you get the following behavior:
print $fh $foo; # not ok
print {$fh} $foo; # ok
print {*STDERR} $foo; # ugly but still ok
print STDERR $foo # much better!
I didn't add any tests, since the worst thing I could imagine is a false
negative if someone wrote something like
sub STDERR (@) { ... }
and I don't see how you'd ever detect that anyway.
--- Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm
2009-09-07 15:37:07.000000000 -0600
+++ /Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm
2010-12-15 17:07:49.000000000 -0700
@@ -28,7 +28,16 @@
#-----------------------------------------------------------------------------
-sub supported_parameters { return () }
+sub supported_parameters {
+ return (
+ {
+ name => 'exclude_special_filehandles',
+ description => 'Allows non-braced uses of the special
global filehandles (STDIN, STDOUT, etc)',
+ default_string => '0',
+ behavior => 'boolean',
+ },
+ );
+}
sub default_severity { return $SEVERITY_LOWEST }
sub default_themes { return qw( core pbp cosmetic ) }
sub applies_to { return 'PPI::Token::Word' }
@@ -67,6 +76,9 @@
return if is_perl_builtin($sib[0]);
return if exists $POSTFIX_WORDS{ $sib[0] };
+ # Allow special filehandles if requested
+ return if $self->{'_exclude_special_filehandles'} &&
is_perl_filehandle($sib[0]);
+
# Second token must be white space
return if !$sib[1]->isa('PPI::Token::Whitespace');
@@ -119,7 +131,14 @@
=head1 CONFIGURATION
-This Policy is not configurable except for the standard options.
+If you find C<print {*STDERR} $foo> to be a little difficult to swallow
+or harder to decipher than just C<print STDERR $foo> but still want to
+encourage C<print {$fh} $foo> there is an option to exclude the
+standard set of special, global filehandles (e.g. STDIN, STDERR, etc):
+C<exclude_special_filehandles>.
+
+ [InputOutput::RequireBracedFileHandleWithPrint]
+ exclude_special_filehandles = 1
=head1 AUTHOR