in reply to Access a global variable from a subroutine when used as "for" iterator
swl has given a great answer that should help you understand the scope of "iterator" (for loop) variables in Perl for loops, but the real issue here is smelly code! Your sub show would be much better to take $i as a parameter so it is clear everywhere show() is used what is being shown:
for $i (1 .. 3) { show($1); } sub show { my ($i) = @_; print "$i\n"; }
That not only fixes the immediate issue, but makes the code clearer and easier to maintain.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Access a global variable from a subroutine when used as "for" iterator
by LanX (Saint) on Dec 20, 2023 at 23:09 UTC | |
by GrandFather (Saint) on Dec 21, 2023 at 02:17 UTC | |
by vitoco (Hermit) on Dec 21, 2023 at 04:49 UTC | |
by vitoco (Hermit) on Dec 21, 2023 at 00:41 UTC | |
by LanX (Saint) on Dec 21, 2023 at 02:44 UTC | |
by vitoco (Hermit) on Dec 21, 2023 at 04:32 UTC | |
by LanX (Saint) on Dec 21, 2023 at 20:51 UTC | |
|