Special modules for special use
In Perl 6, 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
use keyword enables a pragma (similar to how you can
use a module). To disable a pragma, use the
use v6.c; # use 6.c language versionno worries; # don't issue compile time warnings
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.)
This pragma states the version of the compiler that is going to be used, and turns on its features if they are optional.
use v6; # Load latest supported version (non-PREVIEW).# Also, useful for producing better errors when accidentally# executing the program with `perl` instead of `perl6`
use v6.c; # Use the "Christmas" version of Perl 6
use v6.d; # Use the "Diwali" version of Perl 6
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).
This pragma is not currently part of any Perl 6 specification, but is present in Rakudo as a synonym to
use nqp (see below).
Turns on all available
MONKEY pragmas, currently the three above; thus, it would be equivalent to
use MONKEY-TYPING;use MONKEY-SEE-NO-EVAL;use MONKEY-GUTS;
Allows use of experimental features
A lexical pragma that makes Failures returned from routines fatal. For example, prefix
+ on a Str coerces it to Numeric, but will return a Failure if the string contains non-numeric characters. Saving that Failure in a variable prevents it from being sunk, and so the first code block below reaches the
say $x.^name; line and prints
Failure in output.
In the second block, the
use fatal pragma is enabled, so the
say line is never reached because the Exception contained in the Failure returned from prefix
+ gets thrown and the
CATCH block gets run, printing the
Caught... line. Note that both blocks are the same program and
use fatal only affects the lexical block it was used in:
# OUTPUT: «Failure␤»# OUTPUT: «Caught X::Str::Numeric␤»
try blocks, the
fatal pragma is enabled by default, and you can disable it with
try# OUTPUT: «Caught X::Str::Numeric␤»try# OUTPUT: «Failure␤»
[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++ are allowed.
sub abs()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
In this case, providing an
abs sub that doesn't take any arguments, did not make the compilation error go away.
use isms <Perl5>;sub abs()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
This pragma adds subdirectories to the library search path so that the interpreter can find the modules.
use lib <lib /opt/lib /usr/local/lib>;
This will search the directories passed in a list. Please check the modules documentation for more examples.
Set the value of the $?NL constant in the scope it is called. Possible values are
:lf (which is the default, indicating Line Feed),
:crlf (indicating Carriage Return, Line Feed) and
:cr (indicating Carriage Return).
Use at your own risk.
This is a Rakudo-specific pragma. With it, Rakudo provides access to the nqp opcodes in a top level namespace:
use nqp;nqp::say("hello world");
This uses the underlying nqp
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.
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
no precompilation. This will prevent the entire compilation unit (usually a file) from being precompiled.
strict is the default behavior, and requires that you declare variables before using them. You can relax this restriction with
no strict; = 42; # OK
use trace is activated, any line of code executing will be written to STDERR. You can use
no trace to switch off the feature, so this only happens for certain sections of code.
Lexically controls whether compile-time warnings generated by the compiler get shown. Enabled by default.
$ 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 accuratelyat -e:1------> say :foo<>⏏.Pairfoo => Nil$ perl6 -e 'no worries; say :foo<>.Pair'foo => Nil