Variable declaration reset: Difference between revisions

m
→‎{{header|Raku}}: remove an untrue statement, rearrange slightly
m (→‎{{header|Raku}}: remove an untrue statement, rearrange slightly)
Line 163:
<pre></pre>
 
We can however, declare the variable with an "our" scope, which effectively makes it a package global. StillUse needof to'our' suppressscoping theis warningdiscouraged aboutexcept anin uninitializeda variablefew very specific situations. It "works" (for some value of works), but itpollutes the namespace. The 'our' variable will returntrample any other instance of a variable with that name anywhere in the program in any another answerscope.
 
<lang perl6>my @s = 1, 2, 2, 3, 4, 4, 5;
quietly loop (my $i = 0; $i < 7; $i += 1) {
my $curr = @s[$i];
our $prev;
Line 178:
<pre>2
5</pre>
 
Use of 'our' scoping is discouraged except in a few very specific situations. It "works" (for some value of works), but pollutes the namespace. The 'our' variable will trample any other instance of a variable with that name anywhere in the program in any other scope.
 
A better solution is to declare a state variable. A 'state' variable is essentially scoped similar to a 'my' variable (visible only inside the block), but is persistent across calls.
10,343

edits