szabgab has asked for the wisdom of the Perl Monks concerning the following question:
When using strict perl requires to declare even the loop variable of a foreach loop. Why is that? After all the $word variable in the above example is an alias to the values and won't have any effect outside of the loop. Even if written this way:use strict; use 5.010; foreach my $word (qw(abc def ghi)) { say $word; }
Why is this code not allowed?use strict; use 5.010; my $word = "hello"; foreach $word (qw(abc def ghi)) { say $word; } say $word; # word is "hello" here
use strict; use 5.010; foreach $word (qw(abc def ghi)) { say $word; } # generate syntax error if $word is used here
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: why need my in a foreach loop?
by ikegami (Patriarch) on Nov 28, 2010 at 04:55 UTC | |
by 7stud (Deacon) on Nov 28, 2010 at 19:59 UTC | |
by ikegami (Patriarch) on Nov 28, 2010 at 23:23 UTC | |
by 7stud (Deacon) on Dec 02, 2010 at 03:46 UTC | |
by ikegami (Patriarch) on Dec 02, 2010 at 16:38 UTC | |
Re: why need my in a foreach loop?
by moritz (Cardinal) on Nov 28, 2010 at 07:47 UTC | |
by szabgab (Priest) on Nov 28, 2010 at 18:33 UTC | |
by chromatic (Archbishop) on Nov 29, 2010 at 06:18 UTC | |
by TomDLux (Vicar) on Nov 29, 2010 at 04:27 UTC | |
Re: why need my in a foreach loop?
by JavaFan (Canon) on Nov 28, 2010 at 12:08 UTC | |
Re: why need my in a foreach loop?
by PeterPeiGuo (Hermit) on Nov 28, 2010 at 06:09 UTC | |
by ikegami (Patriarch) on Nov 28, 2010 at 08:22 UTC | |
by szabgab (Priest) on Nov 28, 2010 at 18:37 UTC | |
by ikegami (Patriarch) on Nov 28, 2010 at 18:52 UTC | |
by szabgab (Priest) on Dec 08, 2010 at 06:11 UTC | |
|
Back to
Seekers of Perl Wisdom