File tree Expand file tree Collapse file tree 1 file changed +32
-22
lines changed Expand file tree Collapse file tree 1 file changed +32
-22
lines changed Original file line number Diff line number Diff line change @@ -777,28 +777,38 @@ L<function|/language/functions>).
777
777
Like C < my > , C < temp > restores the old value of a variable at the end of its
778
778
scope. However, C < temp > does not create a new variable.
779
779
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>
802
812
803
813
= head2 The C < let > Prefix
804
814
You can’t perform that action at this time.
0 commit comments