| 1 | package Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimitersscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/; |
| 2 | |
| 3 | use 5.010001scip-perl cpan Perl-Critic 1.156 `5.010001`/; |
| 4 | use strictscip-perl cpan Perl-Critic 1.156 strict/; |
| 5 | use warningsscip-perl cpan Perl-Critic 1.156 warnings/; |
| 6 | use Readonlyscip-perl cpan Perl-Critic 1.156 Readonly/; |
| 7 | |
| 8 | use Perl::Critic::Utilsscip-perl cpan Perl-Critic 1.156 Perl/Critic/Utils/ qw{ :booleans :severities hashify }; |
| 9 | |
| 10 | use parentscip-perl cpan Perl-Critic 1.156 parent/ 'Perl::Critic::Policy'; |
| 11 | |
| 12 | our $VERSIONscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/$VERSION. = '1.156'; |
| 13 | |
| 14 | #----------------------------------------------------------------------------- |
| 15 | |
| 16 | Readonly::Scalarscip-perl cpan Perl-Critic 1.156 Readonly/Scalar(). my $DESC => q<Use only '//' or '{}' to delimit regexps>; |
| 17 | Readonly::Scalarscip-perl cpan Perl-Critic 1.156 Readonly/Scalar(). my $EXPL => [246]; |
| 18 | |
| 19 | Readonly::Arrayscip-perl cpan Perl-Critic 1.156 Readonly/Array(). my @EXTRA_BRACKETS => qw{ () [] <> }; |
| 20 | |
| 21 | #----------------------------------------------------------------------------- |
| 22 | |
| 23 | sub supported_parametersscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/supported_parameters(). { |
| 24 | return ( |
| 25 | { |
| 26 | name => 'allow_all_brackets', |
| 27 | description => |
| 28 | q[In addition to allowing '{}', allow '()', '[]', and '{}'.], |
| 29 | behavior => 'boolean', |
| 30 | }, |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | sub default_severityscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/default_severity(). { return $SEVERITY_LOWESTscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/$SEVERITY_LOWEST. } |
| 35 | sub default_themesscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/default_themes(). { return qw( core pbp cosmetic ) } |
| 36 | sub applies_toscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/applies_to(). { return qw(PPI::Token::Regexp::Match |
| 37 | PPI::Token::Regexp::Substitute |
| 38 | PPI::Token::QuoteLike::Regexp) } |
| 39 | |
| 40 | #----------------------------------------------------------------------------- |
| 41 | |
| 42 | sub initialize_if_enabledscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/initialize_if_enabled(). { |
| 43 | my ( $self, undef ) = @_scip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/`@_`.; |
| 44 | |
| 45 | my %delimiters = hashifyscip-perl cpan Perl-Critic 1.156 Perl/Critic/Utils/hashify().( qw< // {} > ); |
| 46 | if ( $self->{_allow_all_brackets} ) { |
| 47 | @delimiters{ @EXTRA_BRACKETS } = (1) x @EXTRA_BRACKETS; |
| 48 | } |
| 49 | |
| 50 | $self->{_allowed_delimiters} = \%delimiters; |
| 51 | |
| 52 | return $TRUEscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/$TRUE.; |
| 53 | } |
| 54 | |
| 55 | #----------------------------------------------------------------------------- |
| 56 | |
| 57 | sub violatesscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/violates(). { |
| 58 | my ( $self, $elem, undef ) = @_scip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/`@_`.; |
| 59 | |
| 60 | my $allowed_delimiters = $self->{_allowed_delimiters}; |
| 61 | foreach my $delimiter ($elem->get_delimitersscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/get_delimiters().()) { |
| 62 | next if $allowed_delimiters->{$delimiter}; |
| 63 | return $self->violationscip-perl cpan Perl-Critic 1.156 Perl/Critic/Policy/RegularExpressions/ProhibitUnusualDelimiters/violation().( $DESC, $EXPL, $elem ); |
| 64 | } |
| 65 | |
| 66 | return; # OK |
| 67 | } |
| 68 | |
| 69 | 1; |
| 70 | |
| 71 | __END__ |
| 72 | |
| 73 | #----------------------------------------------------------------------------- |
| 74 | |
| 75 | =pod |
| 76 | |
| 77 | =head1 NAME |
| 78 | |
| 79 | Perl::Critic::Policy::RegularExpressions::ProhibitUnusualDelimiters - Use only C<//> or C<{}> to delimit regexps. |
| 80 | |
| 81 | |
| 82 | =head1 AFFILIATION |
| 83 | |
| 84 | This Policy is part of the core L<Perl::Critic|Perl::Critic> |
| 85 | distribution. |
| 86 | |
| 87 | |
| 88 | =head1 DESCRIPTION |
| 89 | |
| 90 | Perl lets you delimit regular expressions with almost any character, |
| 91 | but most choices are illegible. Compare these equivalent expressions: |
| 92 | |
| 93 | s/foo/bar/; # good |
| 94 | s{foo}{bar}; # good |
| 95 | s#foo#bar#; # bad |
| 96 | s;foo;bar;; # worse |
| 97 | s|\|\||\||; # eye-gouging bad |
| 98 | |
| 99 | |
| 100 | =head1 CONFIGURATION |
| 101 | |
| 102 | There is one option for this policy, C<allow_all_brackets>. If this |
| 103 | is true, then, in addition to allowing C<//> and C<{}>, the other |
| 104 | matched pairs of C<()>, C<[]>, and C<< <> >> are allowed. |
| 105 | |
| 106 | |
| 107 | =head1 CREDITS |
| 108 | |
| 109 | Initial development of this policy was supported by a grant from the |
| 110 | Perl Foundation. |
| 111 | |
| 112 | |
| 113 | =head1 AUTHOR |
| 114 | |
| 115 | Chris Dolan <[email protected]> |
| 116 | |
| 117 | |
| 118 | =head1 COPYRIGHT |
| 119 | |
| 120 | Copyright (c) 2007-2023 Chris Dolan |
| 121 | |
| 122 | This program is free software; you can redistribute it and/or modify |
| 123 | it under the same terms as Perl itself. The full text of this license |
| 124 | can be found in the LICENSE file included with this module |
| 125 | |
| 126 | =cut |
| 127 | |
| 128 | # Local Variables: |
| 129 | # mode: cperl |
| 130 | # cperl-indent-level: 4 |
| 131 | # fill-column: 78 |
| 132 | # indent-tabs-mode: nil |
| 133 | # c-indentation-style: bsd |
| 134 | # End: |
| 135 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : |