File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/RequireBracedFileHandleWithPrint.pm
Criterion Covered Total %
statement 44 44 100.0
branch 28 30 93.3
condition 6 6 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 93 96 96.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint;
2              
3 40     40   27213 use 5.010001;
  40         189  
4 40     40   243 use strict;
  40         130  
  40         799  
5 40     40   218 use warnings;
  40         108  
  40         969  
6 40     40   225 use Readonly;
  40         108  
  40         2045  
7              
8 40     40   264 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
  40         142  
  40         2139  
9 40     40   15914 use parent 'Perl::Critic::Policy';
  40         115  
  40         312  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Array my @POSTFIX_WORDS => qw( if unless for );
16             Readonly::Hash my %POSTFIX_WORDS => hashify( @POSTFIX_WORDS );
17             Readonly::Scalar my $PRINT_RX => qr/ \A (?: print f? | say ) \z /xms;
18              
19             Readonly::Scalar my $DESC => q{File handle for "print" or "printf" is not braced};
20             Readonly::Scalar my $EXPL => [ 217 ];
21              
22             #-----------------------------------------------------------------------------
23              
24 97     97 0 1710 sub supported_parameters { return () }
25 94     94 1 421 sub default_severity { return $SEVERITY_LOWEST }
26 84     84 1 326 sub default_themes { return qw( core pbp cosmetic ) }
27 38     38 1 141 sub applies_to { return 'PPI::Token::Word' }
28              
29             #-----------------------------------------------------------------------------
30              
31             sub violates {
32 544     544 1 1129 my ( $self, $elem, undef ) = @_;
33              
34 544 100       1816 return if $elem !~ $PRINT_RX;
35 121 100       1046 return if ! is_function_call($elem);
36              
37 119         221 my @sib;
38              
39 119         296 $sib[0] = $elem->snext_sibling();
40 119 100       2313 return if !$sib[0];
41              
42             # Deal with situations where 'print' is called with parentheses
43 117 100       436 if ( $sib[0]->isa('PPI::Structure::List') ) {
44 34         255 my $expr = $sib[0]->schild(0);
45 34 100       506 return if !$expr;
46 26         66 $sib[0] = $expr->schild(0);
47 26 50       352 return if !$sib[0];
48             }
49              
50 109         351 $sib[1] = $sib[0]->next_sibling();
51 109 100       2147 return if !$sib[1];
52 98         306 $sib[2] = $sib[1]->next_sibling();
53 98 100       1891 return if !$sib[2];
54              
55             # First token must be a scalar symbol or bareword;
56 91 100 100     712 return if !( ($sib[0]->isa('PPI::Token::Symbol') && $sib[0] =~ m/\A \$/xms)
      100        
57             || $sib[0]->isa('PPI::Token::Word') );
58              
59             # First token must not be a builtin function or control
60 48 100       384 return if is_perl_builtin($sib[0]);
61 44 100       961 return if exists $POSTFIX_WORDS{ $sib[0] };
62              
63             # Second token must be white space
64 42 100       635 return if !$sib[1]->isa('PPI::Token::Whitespace');
65              
66             # Third token must not be an operator
67 30 100       141 return if $sib[2]->isa('PPI::Token::Operator');
68              
69             # Special case for postfix controls
70 22 100       56 return if exists $POSTFIX_WORDS{ $sib[2] };
71              
72 20 50       286 return if $sib[0]->isa('PPI::Structure::Block');
73              
74 20         79 return $self->violation( $DESC, $EXPL, $elem );
75             }
76              
77             1;
78              
79             __END__
80              
81             #-----------------------------------------------------------------------------
82              
83             =pod
84              
85             =head1 NAME
86              
87             Perl::Critic::Policy::InputOutput::RequireBracedFileHandleWithPrint - Write C<print {$FH} $foo, $bar;> instead of C<print $FH $foo, $bar;>.
88              
89             =head1 AFFILIATION
90              
91             This Policy is part of the core L<Perl::Critic|Perl::Critic>
92             distribution.
93              
94              
95             =head1 DESCRIPTION
96              
97             The C<print> and C<printf> functions have a unique syntax that
98             supports an optional file handle argument. Conway suggests wrapping
99             this argument in braces to make it visually stand out from the other
100             arguments. When you put braces around any of the special
101             package-level file handles like C<STDOUT>, C<STDERR>, and C<DATA>, you
102             must add the C<'*'> sigil or else it won't compile under C<use strict
103             'subs'>.
104              
105             print $FH "Mary had a little lamb\n"; #not ok
106             print {$FH} "Mary had a little lamb\n"; #ok
107              
108             print STDERR $foo, $bar, $baz; #not ok
109             print {STDERR} $foo, $bar, $baz; #won't compile under 'strict'
110             print {*STDERR} $foo, $bar, $baz; #perfect!
111              
112              
113             =head1 CONFIGURATION
114              
115             This Policy is not configurable except for the standard options.
116              
117              
118             =head1 AUTHOR
119              
120             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
121              
122             =head1 COPYRIGHT
123              
124             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
125              
126             This program is free software; you can redistribute it and/or modify
127             it under the same terms as Perl itself. The full text of this license
128             can be found in the LICENSE file included with this module.
129              
130             =cut
131              
132             # Local Variables:
133             # mode: cperl
134             # cperl-indent-level: 4
135             # fill-column: 78
136             # indent-tabs-mode: nil
137             # c-indentation-style: bsd
138             # End:
139             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :