File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm
Criterion Covered Total %
statement 23 29 79.3
branch 1 8 12.5
condition 0 3 0.0
subroutine 11 11 100.0
pod 4 5 80.0
total 39 56 69.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit;
2              
3 40     40   25614 use 5.010001;
  40         220  
4 40     40   290 use strict;
  40         144  
  40         845  
5 40     40   240 use warnings;
  40         128  
  40         1136  
6 40     40   272 use Readonly;
  40         127  
  40         2139  
7              
8 40     40   350 use Perl::Critic::Utils qw{ :characters :severities :classification :ppi };
  40         118  
  40         2211  
9 40     40   22438 use parent 'Perl::Critic::Policy';
  40         180  
  40         300  
10              
11             our $VERSION = '1.150';
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 89     89 0 1589 sub supported_parameters { return () }
21 74     74 1 321 sub default_severity { return $SEVERITY_LOW }
22 84     84 1 371 sub default_themes { return qw(core pbp cosmetic certrule ) }
23 30     30 1 88 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 329     329 1 558 my ( $self, $elem, undef ) = @_;
29              
30 329 50       544 return if $elem->content() ne 'split';
31 0 0         return if ! is_function_call($elem);
32              
33 0           my @args = parse_arg_list($elem);
34 0 0         my $pattern = @args ? $args[0]->[0] : return;
35              
36 0 0 0       if ( $pattern->isa('PPI::Token::Quote') && $pattern->string() ne $SPACE ) {
37 0           return $self->violation( $DESC, $EXPL, $elem );
38             }
39              
40 0           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 :