develooper Front page | perl.beginners | Postings from July 2003

Re: for/foreach question

Thread Previous | Thread Next
From:
david
Date:
July 24, 2003 10:46
Subject:
Re: for/foreach question
Message ID:
20030724174657.76684.qmail@onion.perl.org
Peter Fleck wrote:

> I just stumbled upon this in some perl I'm working on:
> 
> for $arrayref (@datedbi) {
>     #do stuff
> }
> 
> It didn't look right and sure enough, it should be 'foreach'.
> 
> But it worked fine and that's my question - why is this working?
> 
> @datedbi's elements are references to lists and they all seem to be
> getting processed the way I want whether I use 'for' or 'foreach' in
> the code.

Perl internally compiles 'for' into 'foreach' for you:

[panda]$ perl -MO=Deparse -e 'for $i (1..4){;}'
foreach $i (1 .. 4) {
    ();
}
-e syntax OK
[panda]$

they are exactly the same.

david

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About