in reply to Resetting variables
Lexical scoping exists to make your life easier. You don't need to reset variables if you use lexical scoping to your advantage. Here's a very concise example:
use strict; use warnings; my $i = 0; while( $i++ < 10 ) { no warnings qw/uninitialized/; my $scoped; print "\$scoped contains $scoped\n"; $scoped = 100; }
This snippet uses strict and warnings, but for the purposes of this particular demonstration turns off the "uninitialized value" warning. Next, it loops ten times. Each time, it creates a lexically scoped (my) variable called $scope. It prints that variable's contents (which are essentially empty, or undefined). It then assigns the number 100 to $scoped, and loops. If you weren't using lexical scoping, on the second loop iteration, $scoped would still contain 100, and that would print. But as you see if you run this, on each iteration $scoped is still empty. Why? Because at the end of each while block iteration, the lexical variable named $scoped "falls out of scope" (it disappears, forever gone). And on the next iteration, a new one is created (via 'my').
What you need to do is write your script in such a way that when the player decides to play again, all the variables that need to be reset simply fall out of scope. The ones whos values should be kept would need to have been defined at a broader scope.
This sort of thing is described in perlintro, perlsub and perlsyn, for starters. Therein you'll also read about properly passing parameters to subroutines rather than absorbing values through global osmosis. Welcome to a new way of thinking.
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Resetting variables
by yacoubean (Scribe) on Dec 21, 2004 at 17:49 UTC | |
by davido (Cardinal) on Dec 21, 2004 at 18:00 UTC | |
by yacoubean (Scribe) on Dec 21, 2004 at 18:04 UTC | |
by davido (Cardinal) on Dec 21, 2004 at 18:10 UTC | |
by yacoubean (Scribe) on Dec 21, 2004 at 18:36 UTC |