| Perl-Critic documentation | Contained in the Perl-Critic distribution. |
Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr - Use 4-argument substr instead of writing substr($foo, 2, 6) = $bar.
This Policy is part of the core Perl::Critic distribution.
Conway discourages the use of substr() as an lvalue, instead
recommending that the 4-argument version of substr() be used
instead.
substr($something, 1, 2) = $newvalue; # not ok
substr($something, 1, 2, $newvalue); # ok
The four-argument form of substr() was introduced in Perl 5.005.
This policy does not report violations on code which explicitly
specifies an earlier version of Perl (e.g. use 5.004;).
This Policy is not configurable except for the standard options.
"substr" in perlfunc (substr in perlfunc) (or perldoc -f substr).
"4th argument to substr" in perl5005delta (4th argument to substr in perl5005delta)
Graham TerMarsch <graham@howlingfrog.com>
Copyright (c) 2005-2011 Graham TerMarsch. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Perl-Critic documentation | Contained in the Perl-Critic distribution. |
############################################################################## # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitLvalueSubstr.pm $ # $Date: 2011-05-15 16:43:26 -0500 (Sun, 15 May 2011) $ # $Author: clonezone $ # $Revision: 4079 $ ############################################################################## package Perl::Critic::Policy::BuiltinFunctions::ProhibitLvalueSubstr; use 5.006001; use strict; use warnings; use Readonly; use Perl::Critic::Utils qw{ :severities :classification :language }; use base 'Perl::Critic::Policy'; our $VERSION = '1.116'; #----------------------------------------------------------------------------- Readonly::Scalar my $DESC => q{Lvalue form of "substr" used}; Readonly::Scalar my $EXPL => [ 165 ]; Readonly::Scalar my $ASSIGNMENT_PRECEDENCE => precedence_of( q{=} ); Readonly::Scalar my $MINIMUM_PERL_VERSION => version->new( 5.005 ); #----------------------------------------------------------------------------- sub supported_parameters { return () } sub default_severity { return $SEVERITY_MEDIUM } sub default_themes { return qw( core maintenance pbp ) } sub applies_to { return 'PPI::Token::Word' } #----------------------------------------------------------------------------- sub prepare_to_scan_document { my ( $self, $document ) = @_; # perl5005delta says that is when the fourth argument to substr() # was introduced, so ... (RT #59112) my $version = $document->highest_explicit_perl_version(); return ! $version || $version >= $MINIMUM_PERL_VERSION; } #----------------------------------------------------------------------------- sub violates { my ($self, $elem, undef) = @_; return if $elem ne 'substr'; return if ! is_function_call($elem); my $sib = $elem; while ($sib = $sib->snext_sibling()) { if ( $sib->isa( 'PPI::Token::Operator' ) ) { my $rslt = $ASSIGNMENT_PRECEDENCE <=> precedence_of( $sib->content() ); return if $rslt < 0; return $self->violation( $DESC, $EXPL, $sib ) if $rslt == 0; } } return; #ok! } 1; #----------------------------------------------------------------------------- __END__
# Local Variables: # mode: cperl # cperl-indent-level: 4 # fill-column: 78 # indent-tabs-mode: nil # c-indentation-style: bsd # End: # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :