
nj88udd02 at sneakemail
Nov 23, 2009, 5:43 AM
Post #2 of 3
(275 views)
Permalink
|
Hi John, John W. Krahn wrote: > I was thinking about a possible modification for a foreach loop. > > Normally in a foreach loop the list or array is aliased to the scalar > variable after the 'foreach' keyword or to $_ if no variable is > specified, for example: [...] > But suppose that you just wanted to use a foreach loop to count and were > not intending to use the contents of the aliased scalar variable. Could > Perl be modified to do this: > > foreach undef ( 1 .. 5 ) { > # do something five times without alias > } > > Or: > my @array = 'a' .. 'd'; > foreach undef ( @array ) { > # do something @array times without alias > } I think it shouldn't be that hard to do (just guessing), but I don't really see the use case. By simply ignoring $_, you get virtually the same effect. I see two reasons to want this: a) Performance optimization. Seems like a very small win for adding additional syntax b) Propagating $_ from an outer loop. Seems like a really bad use of $_. The outer loop should be using a named loop variable. What's your use case? Cheers, Steffen
|