-
-
15 May 2011 21:59:03 UTC
- Distribution: Perl-Critic
- Module version: 1.116
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Clone repository
- Issues (182)
- Testers (1355 / 56 / 12)
- Kwalitee
Bus factor: 2- License: perl_5
- Activity
24 month- Tools
- Download (622.46KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
and 1 contributors-
Jeffrey Thalhammer
- Dependencies
- B::Keywords
- Carp
- Config::Tiny
- Email::Address
- English
- Exception::Class
- Exporter
- File::Basename
- File::Find
- File::Path
- File::Spec
- File::Spec::Unix
- File::Temp
- Getopt::Long
- IO::String
- IPC::Open2
- List::MoreUtils
- List::Util
- Module::Pluggable
- PPI
- PPI::Document
- PPI::Document::File
- PPI::Node
- PPI::Token::Quote::Single
- PPI::Token::Whitespace
- PPIx::Regexp
- PPIx::Utilities::Node
- PPIx::Utilities::Statement
- Perl::Tidy
- Pod::Parser
- Pod::PlainText
- Pod::Select
- Pod::Spell
- Pod::Usage
- Readonly
- Scalar::Util
- String::Format
- Task::Weaken
- Test::Builder
- Text::ParseWords
- base
- charnames
- overload
- strict
- version
- warnings
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions - Don't modify
$_
in list functions.AFFILIATION
This Policy is part of the core Perl::Critic distribution.
DESCRIPTION
map
,grep
and other list operators are intended to transform arrays into other arrays by applying code to the array elements one by one. For speed, the elements are referenced via a$_
alias rather than copying them. As a consequence, if the code block of themap
orgrep
modify$_
in any way, then it is actually modifying the source array. This IS technically allowed, but those side effects can be quite surprising, especially when the array being passed is@_
or perhapsvalues(%ENV)
! Instead authors should restrict in-place array modification tofor(@array) { ... }
constructs instead, or useList::MoreUtils::apply()
.CONFIGURATION
By default, this policy applies to the following list functions:
map grep List::Util qw(first) List::MoreUtils qw(any all none notall true false firstidx first_index lastidx last_index insert_after insert_after_string)
This list can be overridden the .perlcriticrc file like this:
[ControlStructures::ProhibitMutatingListFunctions] list_funcs = map grep List::Util::first
Or, one can just append to the list like so:
[ControlStructures::ProhibitMutatingListFunctions] add_list_funcs = Foo::Bar::listmunge
LIMITATIONS
This policy deliberately does not apply to
for (@array) { ... }
orList::MoreUtils::apply()
.Currently, the policy only detects explicit external module usage like this:
my @out = List::MoreUtils::any {s/^foo//} @in;
and not like this:
use List::MoreUtils qw(any); my @out = any {s/^foo//} @in;
This policy looks only for modifications of
$_
. Other naughtiness could include modifying$a
and$b
insort
and the like. That's beyond the scope of this policy.SEE ALSO
There is discussion of this policy at http://perlmonks.org/index.pl?node_id=743445.
AUTHOR
Chris Dolan <cdolan@cpan.org>
Michael Wolf <MichaelRWolf@att.net>
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.
Module Install Instructions
To install Perl::Critic, copy and paste the appropriate command in to your terminal.
cpanm Perl::Critic
perl -MCPAN -e shell install Perl::Critic
For more information on module installation, please visit the detailed CPAN module installation guide.