in reply to Code review: function to trim whitespace from all items in a list

The first problem with this code is indeed the fact that grep's purpose is selection, not modification, so it does not help readability. But the problem mentionned by LanX is the worst I guess, that @x is modified under the hood, without it being explicit in the function description is a serious issue.

If modifying @x is OK, then there's no need for another array and s/^\s*|\s*$/g for @x would avoid the copy of the array.

Or, since map can actually do both modification and selection (it can yield a list of one, zero or more values for each input value) here's another way to do the same thing: @y = map { /(\S.*\S)/ ? $1 : () } @x; or if using map to remove elements isn't clear enough: @y = grep defined, map /(\S.*\S)/, @x;

Edit: Whoops, doesn't work for strings that contain only one non-space char. /(\S.*?)\s*$/ would work but is not as easy to read.