in reply to Re: Re: Understanding why strict prevents a use of local
in thread Understanding why strict prevents a use of local
The reason that local causes strict to complain is that local does not declare global variables (strict only complains about global variables if you haven't declared them). The purpose of local is to temporarily hide the original value of the global, so that you don't accidentally overwrite a value that the rest of your program is using. Basically, letting you use a global variable as if it were lexical. (Sort of.)
However, that's not what you want to do. You actually want to access the global value of the variable but you want to declare it, so that strict stops complaining. To do that, remove the locals, and add an our (or use vars for < 5.6) to the beginning of the program:
#!/usr/bin/perl -w use strict; our $i; # Predeclare $i as a global, so # that strict doesn't complain. my @array = qw(one two three); &one; sub one { foreach $i (@array) { &two; } } sub two { print "$i\n"; }
That said, global variables are usually a bad idea, especially in large (or "lengthy") programs. You should probably go with dragonchild's hash suggestion, or something.
bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.
|
|---|