1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringySplit.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit;
15 use Perl::Critic::Utils qw{ :characters :severities :classification :ppi };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{String delimiter used with "split"};
23 Readonly::Scalar my $EXPL => q{Express it as a regex instead};
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_LOW }
29 sub default_themes { return qw(core pbp cosmetic) }
30 sub applies_to { return 'PPI::Token::Word' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
37 return if $elem ne 'split';
38 return if ! is_function_call($elem);
40 my @args = parse_arg_list($elem);
41 my $pattern = @args ? $args[0]->[0] : return;
43 if ( $pattern->isa('PPI::Token::Quote') && $pattern->string() ne $SPACE ) {
44 return $self->violation( $DESC, $EXPL, $elem );
55 #-----------------------------------------------------------------------------
61 Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit - Write C<split /-/, $string> instead of C<split '-', $string>.
65 This Policy is part of the core L<Perl::Critic> distribution.
70 The C<split> function always interprets the PATTERN argument as a
71 regular expression, even if you specify it as a string. This causes
72 much confusion if the string contains regex metacharacters. So for
73 clarity, always express the PATTERN argument as a regex.
75 $string = 'Fred|Barney';
76 @names = split '|', $string; #not ok, is ('F', 'r', 'e', 'd', '|', 'B', 'a' ...)
77 @names = split m/[|]/, $string; #ok, is ('Fred', Barney')
79 When the PATTERN is a single space the C<split> function has special
80 behavior, so Perl::Critic forgives that usage. See C<"perldoc -f
81 split"> for more information.
86 This Policy is not configurable except for the standard options.
91 L<Perl::Critic::Policy::ControlStrucutres::RequireBlockGrep>
93 L<Perl::Critic::Policy::ControlStrucutres::RequireBlockMap>
97 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
101 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
103 This program is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself. The full text of this license
105 can be found in the LICENSE file included with this module.
111 # cperl-indent-level: 4
113 # indent-tabs-mode: nil
114 # c-indentation-style: bsd
116 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :