in reply to A perverse use of grep

my @lines = map { s/^\s+//; s/#.*//; s/\s+$//; length($_) ? $_ : () } +<$in>;
One style rule I strongly recommend is:
Never modify $_ in a map.
If you think of map as transforming one list to another, modifying $_ makes it "unclean", because it also edits the input list! This can lead to subtle bugs, and is best avoided always, rather than permitted occasionally, such as in this code.

So, I'd rewrite that trivially as:

my @lines = map { local $_ = $_; s/^\s+//; s/#.*//; s/\s+$//; length($ +_) ? $_ : () } <$in>;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: modifying $_ in map (was Re: A perverse use of grep)
by johnnywang (Priest) on Jun 15, 2005 at 17:29 UTC
    funny that I just read that section of "Effective Perl" last night (while sitting on my toilet:-). To summarize: use grep to select, use map to transform, in either case, don't change $_. It's interesting to note that the OP is a combination of select and transform.