The standard idiom is:

foreach my $item (@items) {
}

On 19 Jun 2001 14:41:40 -0700, Bob Mangold wrote:
> Thanks, that's what I thought was happening, but now I have another question.
> If I 'use strict' (and who doesn't), I am forced to declare $line before the
> foreach loop, except I can't just type 'foreach my($line) (<>)'. I have to type
> it on a preceeding line. If this is the case then why does perl localize it
> anyway. If i'm declaring it before that loop shouldn't its scope carry through
> the loop?
> 
> -Bob
> 
> 
> 
> --- Jeff 'japhy' Pinyan <[EMAIL PROTECTED]> wrote:
> > On Jun 19, Bob Mangold said:
> > 
> > >I may have a bug somewhere in my code, but I can't find it. Before I
> > >look again though please answer this for me.
> > 
> > >my ($line) = "hello";
> > >foreach $line (<>){
> > >     ..... whatever
> > >}
> > >print $line;
> > >
> > >Should it print the last line in <> or 'hello'? 
> > 
> > I don't think the other responders tested their code.  If they had, they'd
> > see that $line would retain its value.
> > 
> >   my $line = 1;
> >   for $line (1 .. 10) { ; }
> >   print $line;  # 1
> > 
> > This is because the looping variable is implicitly localized to the loop
> > itself.  This is not a bug.
> > 
> > -- 
> > Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
> > I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
> > Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
> > Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
> > Acacia Fraternity, Rensselaer Chapter.         Brother #734
> > **      Manning Publications, Co, is publishing my Perl Regex book      **
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Spot the hottest trends in music, movies, and more.
> http://buzz.yahoo.com/
> 
--
Today is Setting Orange, the 24th day of Confusion in the YOLD 3167
Frink!


Reply via email to