@@ -22,10 +22,53 @@ not yet rendered properly.
22
22
23
23
= item X < B < cache > |cache> [TBD]
24
24
25
- = item X < B < macros > |macros> [TBD]
26
-
27
25
= item X < B < pack > |pack> [TBD]
28
26
27
+ = head2 X < B < macros > |macros>
28
+
29
+ L < Macros|https://en.wikipedia.org/wiki/Macro_(computer_science) > are
30
+ code-generating routines, that generate code in compile time, before the program
31
+ is executed. In Perl 6 its use is still experimental and it needs to be turned
32
+ on via the pragma
33
+
34
+ use experimental :macros;
35
+
36
+ Macro processing happens during parsing time. A macro generates an abstract
37
+ syntax tree, which is grafted into the program syntax tree. C < quasi > is the
38
+ routine that performs this task.
39
+
40
+ = begin code :skip-test<need use experimental>
41
+ macro does-nothing() {
42
+ quasi {}
43
+ };
44
+ does-nothing; # OUTPUT: «»
45
+ = end code
46
+
47
+ X < |quasi (macros) >
48
+ Macros are a kind of routine, so they can take arguments in exactly the same
49
+ way, and act also in almost the same way.
50
+
51
+ = begin code :skip-test<need use experimental>
52
+ macro is-mighty( $who ) {
53
+ quasi { "$who is mighty!"}
54
+ };
55
+ say is-mighty "Freija"; # OUTPUT: « "Freija" is mighty!»
56
+ = end code
57
+
58
+ X < |unquoting (macros) >
59
+ X < |{{{}}} (macros) >
60
+ "Almost" accounts for the fact that the argument is inserted as a literal,
61
+ including the quotes. Please note that we can also eliminate the parentheses for
62
+ a macro call, following the same rules as a routine. You can use the unquoting
63
+ construct C < {{{}}} > to get rid of this kind of thing:
64
+
65
+ = begin code :skip-test<need use experimental>
66
+ macro is-mighty( $who ) {
67
+ quasi { {{{$who}}} ~ " is mighty!"}
68
+ };
69
+ say is-mighty "Freija";# OUTPUT: «Freija is mighty!»
70
+ = end code
71
+
29
72
30
73
= end pod
31
74
0 commit comments