/
pragmas.pod6
304 lines (219 loc) · 8.62 KB
/
pragmas.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
=begin pod :tag<index>
=TITLE Pragmas
=SUBTITLE Special modules that define certain aspects of the behavior of the code
In Perl 6, B<pragmas> are directive used to either identify a specific
version of Perl 6 to be used or to modify the compiler's normal behavior
in some way. The C<use> keyword enables a pragma (similar to how you can
C<use> a module). To disable a pragma, use the C<no> keyword:
=begin code :solo
use v6.c; # use 6.c language version
no worries; # don't issue compile time warnings
=end code
Following is a list of pragmas with a short description of each
pragma's purpose or a link to more details about its use. (Note:
Pragmas marked "[NYI]" are not yet implemented, and those marked "[TBD]"
are to be defined later.)
X<|v6.x, pragma>
=head2 v6.x
This pragma states the version of the compiler that is going to be used,
and turns on its features if they are optional.
=for code :solo
use v6; # Load latest supported version (non-PREVIEW).
# Also, useful for producing better errors when accidentally
# executing the program with `perl` instead of `perl6`
=for code :solo
use v6.c; # Use the "Christmas" version of Perl 6
=for code :solo
use v6.d; # Use the "Diwali" version of Perl 6
From 2018.11, which implemented 6.d, this pragma does not do anything.
=for code :solo
use v6.d.PREVIEW; # On 6.d-capable compilers, enables 6.d features,
# otherwise enables the available experimental
# preview features for 6.d language
Since these pragmas turn on the compiler version, they should be the
first statement in the file (preceding comments and Pod are fine).
X<|MONKEY-GUTS, pragma>
=head2 MONKEY-GUTS
This pragma is not currently part of any Perl 6 specification, but is present
in Rakudo as a synonym to C<use nqp> (see below).
X<|MONKEY-SEE-NO-EVAL, pragma>
=head2 MONKEY-SEE-NO-EVAL
L<EVAL|/routine/EVAL>
X<|MONKEY-TYPING, pragma>
=head2 MONKEY-TYPING
L<augment|/syntax/augment>
X<|MONKEY, pragma>
=head2 MONKEY
C<use MONKEY;>
Turns on all available C<MONKEY>
pragmas, currently the three above; thus, it would be equivalent to
use MONKEY-TYPING;
use MONKEY-SEE-NO-EVAL;
use MONKEY-GUTS;
X<dynamic-scope, pragma>
=head2 dynamic-scope
Applies the L<is dynamic|/type/Variable#trait_is_dynamic> trait to variables
in the pragma's lexical scope. The effect can be restricted to a subset of
variables by listing their names as arguments. By default applies to I<all>
variables.
=begin code :allow<C>
# Apply C<is dynamic> only to $x, but not to $y
use dynamic-scope <$x>;
sub poke {
say $CALLER::x;
say $CALLER::y;
}
my $x = 23;
my $y = 34;
poke;
# OUTPUT:
# 23
# Cannot access '$y' through CALLER, because it is not declared as dynamic
=end code
This pragma is not currently part of any Perl 6 specification and was added
in Rakudo 2019.03.
X<|experimental, pragma>
=head2 experimental
Allows use of L<experimental features|/language/experimental>
X<|fatal, pragma>
=head2 fatal
A lexical pragma that makes L<Failures|/type/Failure> returned from routines
fatal. For example, prefix C<+> on a L<Str|/type/Str> coerces it to
L<Numeric|/type/Numeric>, but will return a L<Failure|/type/Failure> if the
string contains non-numeric characters. Saving that L<Failure|/type/Failure> in
a variable prevents it from being sunk, and so the first code block below
reaches the C<say $x.^name;> line and prints C<Failure> in output.
In the second block, the C<use fatal> pragma is enabled, so the C<say> line is
never reached because the L<Exception|/type/Exception> contained in the L<Failure|/type/Failure> returned from
prefix C<+> gets thrown and the C<CATCH> block gets run, printing the
C<Caught...> line. Note that both blocks are the same program and C<use fatal>
only affects the lexical block it was used in:
{
my $x = +"a";
say $x.^name;
CATCH { default { say "Caught {.^name}" } }
} # OUTPUT: «Failure»
{
use fatal;
my $x = +"a";
say $x.^name;
CATCH { default { say "Caught {.^name}" } }
} # OUTPUT: «Caught X::Str::Numeric»
Inside L«C<try> blocks|/language/exceptions#index-entry-try_blocks-try», the
C<fatal> pragma is enabled by default, and you can I<disable> it with C<no
fatal>:
try {
my $x = +"a";
say $x.^name;
CATCH { default { say "Caught {.^name}" } }
} # OUTPUT: «Caught X::Str::Numeric»
try {
no fatal;
my $x = +"a";
say $x.^name;
CATCH { default { say "Caught {.^name}" } }
} # OUTPUT: «Failure»
X<|internals, pragma>
=head2 internals
[NYI]
X<|invocant, pragma>
=head2 invocant
[NYI]
X<|isms, pragma>
=head2 isms
C<[2018.09 and later]>
Allow for some other language constructs that were deemed to be a trap that
warranted a warning and/or an error in normal Perl 6 programming. Currently,
C<Perl5> and C<C++> are allowed.
=begin code :skip-test<compile-time error>
sub abs() { say "foo" }
abs;
# Unsupported use of bare "abs"; in Perl 6 please use .abs if you meant
# to call it as a method on $_, or use an explicit invocant or argument,
# or use &abs to refer to the function as a noun
=end code
In this case, providing an C<abs> sub that doesn't take any arguments, did
not make the compilation error go away.
use isms <Perl5>;
sub abs() { say "foo" }
abs; # foo
With this, the compiler will allow the offending Perl 5 construct, allowing
the code to actually be executed.
If you do not specify any language, all known language constructs are allowed.
use isms; # allow for Perl5 and C++ isms
X<|lib, pragma>
=head2 lib
This pragma adds subdirectories to the library search
path so that the interpreter can
L<find the modules|/language/modules#Finding_modules>.
=for code :solo
use lib <lib /opt/lib /usr/local/lib>;
This will search the directories passed in a list. Please check
L<the modules documentation|/language/modules#use> for more examples.
X<|newline, pragma>
=head2 newline
Set the value of the L<$?NL|/language/variables#Compile-time_variables> constant
in the scope it is called. Possible values are C<:lf> (which is the default,
indicating Line Feed), C<:crlf> (indicating Carriage Return, Line Feed) and
C<:cr> (indicating Carriage Return).
X<|nqp, pragma>
=head2 nqp
Use at your own risk.
This is a Rakudo-specific pragma. With it, Rakudo provides access to the
L<nqp opcodes|https://github.com/perl6/nqp/blob/master/docs/ops.markdown>
in a top level namespace:
use nqp;
nqp::say("hello world");
This uses the underlying nqp C<say> opcode instead of the Perl 6 routine. This
pragma may make your code rely on a particular version of nqp, and since
that code is not part of the Perl 6 specification, it's not guaranteed to
be stable. You may find a large number of usages in the Rakudo core,
which are used to make the core functionality as fast as possible.
Future optimizations in the code generation of Rakudo may obsolete these
usages.
X<|parameters, pragma>
=head2 parameters
[NYI]
X<|precompilation, pragma>
=head2 precompilation
The default allows precompilation of source code, specifically if used in a
module. If for whatever reason you do not want the code (of your module) to
be precompiled, you can use C<no precompilation>. This will prevent the
entire compilation unit (usually a file) from being precompiled.
X<|soft, pragma>
=head2 soft
L<Re-dispatching|/language/functions#Re-dispatching>, L<inlining|/language/functions#index-entry-use_soft_(pragma)>
X<|strict, pragma>
=head2 strict
C<strict> is the default behavior, and requires that you declare variables
before using them. You can relax this restriction with C<no>.
=for code
no strict; $x = 42; # OK
X<|trace, pragma>
=head2 trace
When C<use trace> is activated, any line of code executing will be written to
STDERR. You can use C<no trace> to switch off the feature, so this only happens
for certain sections of code.
X<|v6, pragma>
=head2 v6
L<Writing Tests|/language/testing#Writing_tests>
X<|variables, pragma>
=head2 variables
L<Defined Variables Pragma|/language/variables#Default_defined_variables_pragma>
X<|worries, pragma>
=head2 worries
Lexically controls whether compile-time warnings generated by the
compiler get shown. Enabled by default.
=begin code :lang<text>
$ perl6 -e 'say :foo<>.Pair'
Potential difficulties:
Pair with <> really means an empty list, not null string; use :foo('') to represent the null string,
or :foo() to represent the empty list more accurately
at -e:1
------> say :foo<>⏏.Pair
foo => Nil
$ perl6 -e 'no worries; say :foo<>.Pair'
foo => Nil
=end code
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6