@@ -69,7 +69,37 @@ macro is-mighty( $who ) {
69
69
say is-mighty "Freija";# OUTPUT: «Freija is mighty!»
70
70
= end code
71
71
72
+ Since macro expansion happens at parse time, care must be taken when using
73
+ external variables in them:
74
+
75
+ = begin code :skip-test<need use experimental>
76
+ use experimental :macros;
77
+ my $called;
78
+ macro called() {
79
+ $called++;
80
+ quasi { "Called" }
81
+ };
82
+ say called() ~ " $called times";
83
+ say called() ~ " $called times"; # OUTPUT: «Called 2 timesCalled 2 times»
84
+ = end code
85
+
86
+ Since macros are expanded at parse time, C < $called > will be the result when run
87
+ time starts, which is already C < 2 > , as printed. Initializing C < $called > with 0,
88
+ however, will make this print C < Called 0 times > since that initialization is run
89
+ I < after > the parse phase, when the macros are expanded.
90
+
91
+ Macros are terribly useful when complicated, computed initializations need to be
92
+ done. However, they are still in the I < experimental > nursery for a good reason.
93
+ Although the features shown above are not very likely to change, anything, even
94
+ their very presence, might change at any one time depending in necessities, so
95
+ it would be best to keep them away from production code. Meanwhile, taking a
96
+ look at
97
+ L < this article by Masak|https://perl6advent.wordpress.com/2012/12/23/day-23-macros/ >
98
+ as well as
99
+ L < C < 007 > |https://github.com/masak/007> , a new macro language, which might show
100
+ the shape of things to come.
72
101
73
102
= end pod
74
103
104
+
75
105
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
0 commit comments