.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions 3pm" .TH Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions 3pm 2023-07-26 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions \- Don't modify "$_" in list functions. .SH AFFILIATION .IX Header "AFFILIATION" This Policy is part of the core Perl::Critic distribution. .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`map\*(C'\fR, \f(CW\*(C`grep\*(C'\fR 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 \f(CW$_\fR alias rather than copying them. As a consequence, if the code block of the \f(CW\*(C`map\*(C'\fR or \f(CW\*(C`grep\*(C'\fR modify \f(CW$_\fR 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 \f(CW@_\fR or perhaps \f(CWvalues(%ENV)\fR! Instead authors should restrict in-place array modification to \f(CW\*(C`for(@array) { ... }\*(C'\fR constructs instead, or use \f(CWList::SomeUtils:apply()\fR or \f(CWList::MoreUtils::apply()\fR. .SH CONFIGURATION .IX Header "CONFIGURATION" By default, this policy applies to the following list functions: .PP .Vb 8 \& 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) \& List::SomeUtils qw(any all none notall true false firstidx \& first_index lastidx last_index insert_after \& insert_after_string) .Ve .PP This list can be overridden the \fI.perlcriticrc\fR file like this: .PP .Vb 2 \& [ControlStructures::ProhibitMutatingListFunctions] \& list_funcs = map grep List::Util::first .Ve .PP Or, one can just append to the list like so: .PP .Vb 2 \& [ControlStructures::ProhibitMutatingListFunctions] \& add_list_funcs = Foo::Bar::listmunge .Ve .SH LIMITATIONS .IX Header "LIMITATIONS" This policy deliberately does not apply to \f(CW\*(C`for (@array) { ... }\*(C'\fR or \&\f(CWList::MoreUtils::apply()\fR \f(CWList::SomeUtils::apply()\fR. .PP Currently, the policy only detects explicit external module usage like this: .PP .Vb 2 \& my @out = List::MoreUtils::any {s/^foo//} @in; \& my @out = List::SomeUtils::any {s/^foo//} @in; .Ve .PP and not like this: .PP .Vb 2 \& use List::MoreUtils qw(any); \& my @out = any {s/^foo//} @in; \& \& use List::SomeUtils qw(any); \& my @out = any {s/^foo//} @in; .Ve .PP This policy looks only for modifications of \f(CW$_\fR. Other naughtiness could include modifying \f(CW$a\fR and \f(CW$b\fR in \f(CW\*(C`sort\*(C'\fR and the like. That's beyond the scope of this policy. .SH "SEE ALSO" .IX Header "SEE ALSO" There is discussion of this policy at . .SH AUTHOR .IX Header "AUTHOR" Chris Dolan .PP Michael Wolf .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (c) 2006\-2021 Chris Dolan. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.