1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint;
15 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Array my @POSTFIX_WORDS => qw( if unless for );
23 Readonly::Hash my %POSTFIX_WORDS => hashify( @POSTFIX_WORDS );
25 Readonly::Scalar my $DESC => q{File handle for "print" is not braced};
26 Readonly::Scalar my $EXPL => [ 217 ];
28 #-----------------------------------------------------------------------------
30 sub supported_parameters { return () }
31 sub default_severity { return $SEVERITY_LOWEST }
32 sub default_themes { return qw( core pbp cosmetic ) }
33 sub applies_to { return 'PPI::Token::Word' }
35 #-----------------------------------------------------------------------------
38 my ( $self, $elem, undef ) = @_;
40 return if $elem ne 'print';
41 return if ! is_function_call($elem);
45 $sib[0] = $elem->snext_sibling();
48 # Deal with situations where 'print' is called with parentheses
49 if ( $sib[0]->isa('PPI::Structure::List') ) {
50 my $expr = $sib[0]->schild(0);
52 $sib[0] = $expr->schild(0);
56 $sib[1] = $sib[0]->next_sibling();
58 $sib[2] = $sib[1]->next_sibling();
61 # First token must be a scalar symbol or bareword;
62 return if !( ($sib[0]->isa('PPI::Token::Symbol') && $sib[0] =~ m/\A \$/mx)
63 || $sib[0]->isa('PPI::Token::Word') );
65 # First token must not be a builtin function or control
66 return if is_perl_builtin($sib[0]);
67 return if exists $POSTFIX_WORDS{ $sib[0] };
69 # Second token must be white space
70 return if !$sib[1]->isa('PPI::Token::Whitespace');
72 # Third token must not be an operator
73 return if $sib[2]->isa('PPI::Token::Operator');
75 # Special case for postfix controls
76 return if exists $POSTFIX_WORDS{ $sib[2] };
78 return if $sib[0]->isa('PPI::Structure::Block');
80 return $self->violation( $DESC, $EXPL, $elem );
87 #-----------------------------------------------------------------------------
93 Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint - Write C<print {$FH} $foo, $bar;> instead of C<print $FH $foo, $bar;>.
97 This Policy is part of the core L<Perl::Critic> distribution.
102 The C<print> function has a unique syntax that supports an optional
103 file handle argument. Conway suggests wrapping this argument in
104 braces to make it visually stand out from the other arguments. When
105 you put braces around any of the special package-level file handles
106 like C<STDOUT>, C<STDERR>, and C<DATA>, you must the C<'*'> sigil or
107 else it won't compile under C<use strict 'subs'>.
109 print $FH "Mary had a little lamb\n"; #not ok
110 print {$FH} "Mary had a little lamb\n"; #ok
112 print STDERR $foo, $bar, $baz; #not ok
113 print {STDERR} $foo, $bar, $baz; #won't compile under 'strict'
114 print {*STDERR} $foo, $bar, $baz; #perfect!
119 This Policy is not configurable except for the standard options.
124 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
128 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
130 This program is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself. The full text of this license
132 can be found in the LICENSE file included with this module.
138 # cperl-indent-level: 4
140 # indent-tabs-mode: nil
141 # c-indentation-style: bsd
143 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :