1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitReverseSortBlock;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Forbid $b before $a in sort blocks}; ## no critic (Interpolation)
23 Readonly::Scalar my $EXPL => [ 152 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_LOWEST }
29 sub default_themes { return qw(core pbp cosmetic) }
30 sub applies_to { return 'PPI::Token::Word' }
32 #-----------------------------------------------------------------------------
35 my ($self, $elem, $doc) = @_;
37 return if $elem ne 'sort';
38 return if ! is_function_call($elem);
40 my $sib = $elem->snext_sibling();
44 if ( $arg->isa('PPI::Structure::List') ) {
45 $arg = $arg->schild(0);
46 # Forward looking: PPI might change in v1.200 so schild(0) is a PPI::Statement::Expression
47 if ( $arg && $arg->isa('PPI::Statement::Expression') ) {
48 $arg = $arg->schild(0);
51 return if !$arg || !$arg->isa('PPI::Structure::Block');
53 # If we get here, we found a sort with a block as the first arg
55 # Look at each statement in the block separately.
56 # $a is +1, $b is -1, sum should always be >= 0.
57 # This may go badly if there are conditionals or loops or other
59 for my $statement ($arg->children) {
60 my @sort_vars = $statement =~ m/\$([ab])\b/gxms;
62 for my $sort_var (@sort_vars) {
63 if ($sort_var eq 'a') {
68 # Found too many C<$b>s too early
69 my $sev = $self->get_severity();
70 return $self->violation( $DESC, $EXPL, $elem, $sev );
80 #-----------------------------------------------------------------------------
88 Perl::Critic::Policy::BuiltinFunctions::ProhibitReverseSortBlock - Forbid $b before $a in sort blocks.
92 This Policy is part of the core L<Perl::Critic> distribution.
97 Conway says that it is much clearer to use C<reverse> than to flip C<$a> and
98 C<$b> around in a C<sort> block. He also suggests that, in newer perls,
99 C<reverse> is specifically looked for and optimized, and in the case of a
100 simple reversed string C<sort>, using C<reverse> with a C<sort> with no block
101 is faster even in old perls.
103 my @foo = sort { $b cmp $a } @bar; #not ok
104 my @foo = reverse sort @bar; #ok
106 my @foo = sort { $b <=> $a } @bar; #not ok
107 my @foo = reverse sort { $a <=> $b } @bar; #ok
112 This Policy is not configurable except for the standard options.
117 Chris Dolan <cdolan@cpan.org>
121 Copyright (C) 2006 Chris Dolan. All rights reserved.
123 This program is free software; you can redistribute it and/or modify
124 it under the same terms as Perl itself.
130 # cperl-indent-level: 4
132 # indent-tabs-mode: nil
133 # c-indentation-style: bsd
135 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :