Skip to content

Commit 8660e53

Browse files
committed
better example for C<temp>
1 parent 4a47be5 commit 8660e53

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

doc/Language/variables.pod

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -777,28 +777,38 @@ L<function|/language/functions>).
777777
Like C<my>, C<temp> restores the old value of a variable at the end of its
778778
scope. However, C<temp> does not create a new variable.
779779
780-
=begin code
781-
my $cookies = 5;
782-
783-
sub buy-cookie { $cookies++ }
784-
785-
{
786-
my $cookies = 42;
787-
buy-cookie; # Increments the outer $cookies variable
788-
say $cookies; # 42
789-
# the outer $cookies variable is 6 now
790-
}
791-
792-
{
793-
temp $cookies = 42;
794-
# Still the same $cookies, but a new value:
795-
buy-cookie;
796-
say $cookies; # -> 43
797-
}
798-
799-
# Old value is restored
800-
say $cookies; # -> 6
801-
=end code
780+
my $in = 0; # temp will "entangle" the global variable with the call stack
781+
# that keeps the calls at the bottom in order.
782+
sub f(*@c) {
783+
(temp $in)++;
784+
"<f>\n"
785+
~ @c>>.indent($in).join("\n")
786+
~ (+@c ?? "\n" !! "")
787+
~ '</f>'
788+
};
789+
sub g(*@c) {
790+
(temp $in)++;
791+
"<g>\n"
792+
~ @c>>.indent($in).join("\n")
793+
~ (+@c ?? "\n" !! "")
794+
~ "</g>"
795+
};
796+
print g(g(f(g()),g(),f()));
797+
798+
output:
799+
800+
<g>
801+
<g>
802+
<f>
803+
<g>
804+
</g>
805+
</f>
806+
<g>
807+
</g>
808+
<f>
809+
</f>
810+
</g>
811+
</g>
802812
803813
=head2 The C<let> Prefix
804814

0 commit comments

Comments
 (0)