GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  PERL::CRITIC::POLICY::VARIABLES::REQUIRENEGATIVEINDICES (3)

.ds Aq ’

NAME

Perl::Critic::Policy::Variables::RequireNegativeIndices - Negative array index should be used.

CONTENTS

AFFILIATION

This Policy is part of the core Perl::Critic distribution.

DESCRIPTION

Perl 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 <B>their semantics changeB> 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];



CONFIGURATION

This Policy is not configurable except for the standard options.

AUTHOR

Chris Dolan <cdolan@cpan.org>

COPYRIGHT

Copyright (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.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 PERL::CRITIC::POLICY::VARIABLES::REQUIRENEGATIVEINDICES (3) 2016-04-03

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.