The following perl code generates a warning in PerlCritic (by Activestate):
sub natural_sort {
my @sorted;
@sorted = grep {s/(^|\D)0+(\d)/$1$2/g,1} sort grep {s/(\d+)/sprintf"%06.6d",$1/ge,1} @_;
}
The warning generated is:
Don't modify $_ in list functions
More info about that warning here
I don't understand the warning because I don't think I'm modifying $_, although I suppose I must be. Can someone explain it to me please?
Both of your
greps are modifying$_because you're usings//. For example, this:is the same as this:
I think you'd be better off using
mapas you are not filtering anything with yourgreps, you're just usinggrepas an iterator:That should do the same thing and keep critic quiet. You might want to have a look at
List::MoreUtilsif you want some nicer list operators than plainmap.