File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 55 56 98.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit;
2              
3 40     40   25723 use 5.010001;
  40         222  
4 40     40   268 use strict;
  40         114  
  40         881  
5 40     40   247 use warnings;
  40         130  
  40         985  
6 40     40   253 use Readonly;
  40         194  
  40         2135  
7              
8 40     40   283 use Perl::Critic::Utils qw{ :characters :severities :classification :ppi };
  40         152  
  40         2261  
9 40     40   22217 use parent 'Perl::Critic::Policy';
  40         171  
  40         286  
10              
11             our $VERSION = '1.148';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{String delimiter used with "split"};
16             Readonly::Scalar my $EXPL => q{Express it as a regex instead};
17              
18             #-----------------------------------------------------------------------------
19              
20 93     93 0 1650 sub supported_parameters { return () }
21 86     86 1 411 sub default_severity { return $SEVERITY_LOW }
22 84     84 1 363 sub default_themes { return qw(core pbp cosmetic certrule ) }
23 34     34 1 122 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 366     366 1 745 my ( $self, $elem, undef ) = @_;
29              
30 366 100       722 return if $elem->content() ne 'split';
31 37 100       243 return if ! is_function_call($elem);
32              
33 36         101 my @args = parse_arg_list($elem);
34 36 100       99 my $pattern = @args ? $args[0]->[0] : return;
35              
36 35 100 100     170 if ( $pattern->isa('PPI::Token::Quote') && $pattern->string() ne $SPACE ) {
37 12         121 return $self->violation( $DESC, $EXPL, $elem );
38             }
39              
40 23         195 return; #ok
41             }
42              
43              
44             1;
45              
46             __END__
47              
48             #-----------------------------------------------------------------------------
49              
50             =pod
51              
52             =head1 NAME
53              
54             Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit - Write C<split /-/, $string> instead of C<split '-', $string>.
55              
56              
57             =head1 AFFILIATION
58              
59             This Policy is part of the core L<Perl::Critic|Perl::Critic>
60             distribution.
61              
62              
63             =head1 DESCRIPTION
64              
65             The C<split> function always interprets the PATTERN argument as a
66             regular expression, even if you specify it as a string. This causes
67             much confusion if the string contains regex metacharacters. So for
68             clarity, always express the PATTERN argument as a regex.
69              
70             $string = 'Fred|Barney';
71             @names = split '|', $string; #not ok, is ('F', 'r', 'e', 'd', '|', 'B', 'a' ...)
72             @names = split m/[|]/, $string; #ok, is ('Fred', Barney')
73              
74             When the PATTERN is a single space the C<split> function has special
75             behavior, so Perl::Critic forgives that usage. See C<"perldoc -f
76             split"> for more information.
77              
78              
79             =head1 CONFIGURATION
80              
81             This Policy is not configurable except for the standard options.
82              
83              
84             =head1 SEE ALSO
85              
86             L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep|Perl::Critic::Policy::BuiltinFunctions::RequireBlockGrep>
87              
88             L<Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap|Perl::Critic::Policy::BuiltinFunctions::RequireBlockMap>
89              
90              
91             =head1 AUTHOR
92              
93             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
94              
95              
96             =head1 COPYRIGHT
97              
98             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
99              
100             This program is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself. The full text of this license
102             can be found in the LICENSE file included with this module.
103              
104             =cut
105              
106             # Local Variables:
107             # mode: cperl
108             # cperl-indent-level: 4
109             # fill-column: 78
110             # indent-tabs-mode: nil
111             # c-indentation-style: bsd
112             # End:
113             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :