Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

Possible foreach loop modification?

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


jwkrahn at shaw

Nov 21, 2009, 9:01 PM

Post #1 of 3 (296 views)
Permalink
Possible foreach loop modification?

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:

foreach $var ( @array | LIST ) {
# $var aliased to @array | LIST elements
}

foreach ( @array | LIST ) {
# $_ aliased to @array | LIST elements
}


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
}


Just asking. :-)


John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway


nj88udd02 at sneakemail

Nov 23, 2009, 5:43 AM

Post #2 of 3 (275 views)
Permalink
Re: Possible foreach loop modification? [In reply to]

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


jwkrahn at shaw

Nov 23, 2009, 7:34 AM

Post #3 of 3 (266 views)
Permalink
Re: Possible foreach loop modification? [In reply to]

Steffen Mueller wrote:
> 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?

I was just musing on the possibility of removing the aliasing of $_ when
it is not required which may lead to safer and/or faster code.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.