Skip to content

Commit 3d96dad

Browse files
committed
Completed macro intro. Refs #2514
1 parent dff5b3e commit 3d96dad

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

doc/Language/experimental.pod6

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,37 @@ macro is-mighty( $who ) {
6969
say is-mighty "Freija";# OUTPUT: «Freija is mighty!␤»
7070
=end code
7171
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 times␤Called 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.
72101
73102
=end pod
74103

104+
75105
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)