Query: perl::critic::policy::variables::requirenegativeindices
OS: centos
Section: 3
Format: Original Unix Latex Style Formatted with HTML and a Horizontal Scroll Bar
Perl::Critic::Policy::Variables::RequireNegativeIndices(User Contributed Perl DocumentatPerl::Critic::Policy::Variables::RequireNegativeIndices(3)NAMEPerl::Critic::Policy::Variables::RequireNegativeIndices - Negative array index should be used.AFFILIATIONThis Policy is part of the core Perl::Critic distribution.DESCRIPTIONPerl treats a negative array subscript as an offset from the end. Given this, the preferred way to get the last element is $x[-1], not $x[$#x] or $x[@x-1], and the preferred way to get the next-to-last is $x[-2], not "$x[$#x-1" or $x[@x-2]. The biggest argument against the non-preferred forms is that their semantics change when the computed index becomes negative. If @x contains at least two elements, $x[$#x-1] and $x[@x-2] are equivalent to $x[-2]. But if it contains a single element, $x[$#x-1] and $x[@x-2] are both equivalent to $x[-1]. Simply put, the preferred form is more likely to do what you actually want. As Conway points out, the preferred forms also perform better, are more readable, and are easier to maintain. This policy notices all of the simple forms of the above problem, but does not recognize any of these more complex examples: $some->[$data_structure]->[$#{$some->[$data_structure]} -1]; my $ref = @arr; $ref->[$#arr];CONFIGURATIONThis Policy is not configurable except for the standard options.AUTHORChris Dolan <cdolan@cpan.org>COPYRIGHTCopyright (c) 2006-2011 Chris Dolan. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.3 2014-06-09 Perl::Critic::Policy::Variables::RequireNegativeIndices(3)
Similar Topics in the Unix Linux Community |
---|
to assign cut values to an array |
Comparing Variables in Perl |
Grabbing web forms with Perl |
Perl: How to check whether my array contains element x |
Perl : Perl equivalent to the ksh | and ; |