Re: [NEWBIE] newline question

From: Randal L. Schwartz (merlyn_at_stonehenge.com)
Date: 03/31/04


Date: 31 Mar 2004 13:52:28 -0800


*** post for FREE via your newsreader at post.newsfeed.com ***

>>>>> "Gunnar" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:

Gunnar> That works, but it's clearer written as:

Gunnar> @lines = map { tr/\n//d; $_ } @lines;

I don't consider that clearer. The loop first modifies @lines (via
the side effect of having changed $_ in the map block), then gathers
all those results together to create a new list, then assigns the
entire new list over the top of the identically updated list.

Weird. Definitely not clearer, and more dangerous too. Consider
the obvious cut-and-paste mangling:

@newlines = map { tr/\n//d; $_ } @lines;

Your copy of @lines and @newlines are identical, even though you might
expect @lines to remain unaffected!

Definitely bad. Definitely don't do this. Not without the required
BIG HONKIN COMMENT to the right describing how wasteful you are.

print "Just another Perl hacker,"; # yeah, the guy who invented this phrase

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----