DESCRIPTION§
A (hopefully) comprehensive list of Perl Special Variables with their Raku equivalents with notes on variations between them where necessary.
NOTE§
This document is an attempt to guide the reader from the Special Variables in Perl to their equivalents in Raku. For full documentation on the Raku Special Variables, please see the Raku documentation for each of them.
SPECIAL VARIABLES§
General variables§
$ARG, $_§
Thankfully, $_
is the general default variable as in Perl. The main difference in Raku is that you can now call methods on it. For instance, Perl's say $_
can be rendered in Raku as $_.say
. Furthermore, as it is the default variable, you don't even need to use the variable name. The previous example can also be achieved by using .say
.
@ARG, @_§
As Raku now has function signatures, your arguments can go there, rather than depending on @_
for them. In fact, if you use a function signature, use of @_
will spit at you telling it cannot override an existing signature.
If, however, you do not use a function signature, @_
will contain the arguments you pass to the function as it did in Perl. Again, as with $_
, you can call methods on it. Unlike $_
you cannot assume @_
as the default variable for those methods to operate on (i. e. @_.shift
works, .shift
does not).
$LIST_SEPARATOR, $"§
Currently, there is no equivalent of the List Separator variable in Raku. Design document S28 says there isn't one, so you probably don't want to hold your breath.
$PROCESS_ID, $PID, $$§
$$
is replaced in Raku by $*PID
$PROGRAM_NAME, $0§
You can access the program name in Raku via $*PROGRAM-NAME
.
Note: $0
in Raku is the variable holding the first captured value from a regexp match (i. e. capture variables now start from $0
rather than $1
).
$REAL_GROUP_ID, $GID, $(§
In Raku the group information is handled by $*GROUP
, which holds an object of type IntStr
and therefore can be used either within a string or a numeric context. The group id is therefore obtained via +$*GROUP
, while the group name via ~$*GROUP
.
$EFFECTIVE_GROUP_ID, $EGID, $)§
The effective group id does not appear to be currently provided by Raku.
$REAL_USER_ID, $UID, $<§
In Raku the user information is handled by $*USER
, which holds an object of type IntStr
and therefore can be used either within a string or a numeric context (this is similar to how the group information is handled by the $*GROUP
object). The user id is therefore obtained via +$*USER
, while the username via ~$*USER
.
$EFFECTIVE_USER_ID, $EUID, $>§
The effective user id is not provided by Raku.
$SUBSCRIPT_SEPARATOR, $SUBSEP, $;§
The subscript separator variable is not included in Raku. Frankly, if your Perl code is using this, it's almost certainly really, really old.
$a, $b§
$a
and $b
have no special meaning in Raku. sort()
does not use them for anything special. They're just regular old variables.
This feature has been extended by having blocks with placeholder parameters which are more versatile. Placeholder variables are created with the ^
twigil (e. g. $^z
. They can be used in a bare block or in a subroutine without an explicit parameter list. The arguments to the block are assigned to the placeholder variables in their Unicode order. I. e. even if the variables appear in the block in the order ($^q, $^z, $^a)
, they will be assigned in the order ($^a, $^q, $^z)
. Ergo:
sort , 1, 5, 6, 4, 2, 3;# OUTPUT: «(1 2 3 4 5 6)»sort , 1, 5, 6, 4, 2, 3;# OUTPUT: «(6 5 4 3 2 1)»for 1..9# OUTPUT: «312»
For more on placeholder variables, see this page
%ENV§
%ENV has been replaced by %*ENV in Raku. Note that the keys of this hash may not be exactly the same between Perl and Raku. For example, OLDPWD
is missing from Raku's %ENV.
$OLD_PERL_VERSION, $]§
The running version of Raku is kept by $*RAKU
special variable, that is an object. The running version is retrieved via $*RAKU.version
, which returns something like v6.d
; the full stringified version of the Raku interpreter is obtained via $*RAKU.gist
, which returns something like Raku (6.d)
.
$SYSTEM_FD_MAX, $^F§
No longer exists in Raku.
@F§
[NEEDS FURTHER RESEARCH] A bit confusing at this point. Design doc S28 indicates that @F
in Perl is replaced by @_
in Raku, but it's unclear just how that works. On the other hand, it's currently something of a moot point, as the Perl to Raku Translation doc indicates that the -a
and -F
command-line switches are not yet implemented in rakudo.
@INC§
No longer exists in Raku. Please use "use lib" to manipulate the module repositories to be searched. The closest thing to @INC is really $*REPO. But that works completely differently from @INC mostly because of the precompilation capabilities of Raku.
# Print out a list of compunit repositories.say for .repo-chain;
%INC§
No longer exists in Raku. Because each Repository is responsible for remembering which modules have been loaded already. You can get a list of all loaded modules (compilation units) like so:
use Test;use MyModule;say flat .repo-chain.map(*.loaded); #-> (MyModule Test)
$INPLACE_EDIT, $^I§
S28 suggests $*INPLACE_EDIT, but it does not yet exist.
$^M§
S28 suggests $*EMERGENCY_MEMORY, but it does not yet exist.
$OSNAME, $^O§
This is somewhat unclear. It probably depends on what you mean by "the name of the operating system" as design document S28 has three different suggestions, all of which give different answers.
There are currently three main objects containing information about the "running environment":
$*KERNEL
provides information about the running Operating System kernel;$*DISTRO
provides information about the Operating System distribution;$*VM
provides information about the running backend machine for Raku.
All the above objects have methods in common:
version
provides the version number for that component;name
provides the mnemonic name for that component;auth
provides the known author(s) for that component.
As a short example, the following piece of code prints information about all the above components:
for , , -># Kernel# version 4.10.0.42.generic named linux by unknown# Distro# version 17.04.Zesty.Zapus named ubuntu by https://www.ubuntu.com/# VM# version 2017.11 named moar by The MoarVM Team
The Str
method on all of the above produces the short version of the information, at the current time the name
.
All the objects have other methods that can be useful when trying to identify the exact running instance, for more information use .^methods
to introspect all the above.
%SIG§
No equivalent variable. To have your code executed on the reception of a signal, you can call the signal subroutine, which returns a Supply
that can be tapped.
$SIG{"INT"} = sub { say "bye"; exit }
signal(SIGINT).tap: ; loop
Or, if you have a generic code that want to know which signal it got:
signal(SIGINT).tap: -> ; loop
A more idiomatic way of using signals in an event driven situation:
react
$BASETIME, $^T§
Replaced in Raku by $*INIT-INSTANT
. Unlike in Perl, this is not in seconds since epoch, but an Instant
object, which is measured in atomic seconds, with fractions.
$PERL_VERSION, $^V§
As with $]
this has been replaced with $*RAKU.version
.
${^WIN32_SLOPPY_STAT}§
There is no analog to this in Raku.
$EXECUTABLE_NAME, $^X§
This has been replaced by $*EXECUTABLE-NAME
. Note that there is also $*EXECUTABLE
, which is an IO
object in Raku.
Variables related to regular expressions§
Performance issues§
As shown below, $`
, $&
, and $'
are gone from Raku, primarily replaced by variations on $/
and, with their elimination, the associated performance issues in Perl do not apply.
$<digits> ($1, $2, ...)§
These existing variables do the same thing in Raku as they do in Perl, except that they now start at $0
rather than $1
. Furthermore, they are synonyms for indexed items in the match variable $/
. I. e. $0
is equivalent to $/[0]
, $1
is equivalent to $/[1]
, etc.
$MATCH, $&§
$/
now contains the Match
object, so the Perl behavior of $&
can be obtained by stringifying it, i. e. ~$/
.
Please note that while $/.Str
should also work, ~$/
is currently the more common idiom.
${^MATCH}§
Since the former performance issues are done away with, this variable is not of use in Raku.
$PREMATCH, $`§
Replaced by $/.prematch
.
${^PREMATCH}§
Since the former performance issues are done away with, this variable is not of use in Raku.
$POSTMATCH, $'§
Replaced by $/.postmatch
.
${^POSTMATCH}§
Since the former performance issues are done away with, this variable is not of use in Raku.
$LAST_PAREN_MATCH, $+§
Does not exist in Raku, but you can get the same information using $/[*- 1].Str
($/[*-1]
would be the match object, not the actual string).
If you want to understand why that works, you can look at these documents:
[ ] routine, Whatever
, and the historical design document
$LAST_SUBMATCH_RESULT, $^N§
S28 suggests $*MOST_RECENT_CAPTURED_MATCH
, but there does not seem to be any implemented variable that matches $^N
.
@LAST_MATCH_END, @+§
As with most regular expression related variables, this functionality is, at least in part, moved to the $/
variable in Raku. Or, in this case, the numbered variables that alias to the indexes of it. The offset is found by using the .to
method. I. e. the first offset is $/[0].to
, which is synonymous with $0.to
. The value Perl provides as $+[0]
is provided by $/.to
.
%LAST_PAREN_MATCH, %+§
Once again, we move over to $/
. The former $+{$match}
is $/{$match}
.
@LAST_MATCH_START, @-§
Similarly to @+
being replaced by using the .to
method, @-
is replaced by using the .from
method on $/
and its variations. The first offset is $/[0].from
or the equivalent $0.from
. Perl's $- [0]
is $/.from
.
%LAST_MATCH_START, %-§
Much like %+
, a use of %-{$match}
would be replaced with $/{$match}
.
$LAST_REGEXP_CODE_RESULT, $^R§
No equivalent.
${^RE_DEBUG_FLAGS}§
No equivalent.
${^RE_TRIE_MAXBUF}§
No equivalent.
Variables related to filehandles§
$ARGV§
The name of the current file when reading lines can be obtained through $*ARGFILES.path
.
@ARGV§
@*ARGS
contains the command line arguments.
ARGV§
This has been replaced by $*ARGFILES
.
ARGVOUT§
As the -i
command line switch has not yet been implemented, there is not yet an equivalent of ARGVOUT
.
$OUTPUT_FIELD_SEPARATOR, $OFS, $,§
Currently no obvious equivalent.
$INPUT_LINE_NUMBER§
$NR, $.§
No direct replacement exists.
When iterating using lines method from IO::Path
or IO::Handle
types, you can call the .kv
method on it to get an interleaved list of indexes and values (then iterate by 2 each loop):
for "foo".IO.lines.kv -> ,# OUTPUT:# 1: a# 2: b# 3: c# 4: d
For IO::CatHandle
types (of which $*ARGFILES
is one), you can use on-switch
hook to reset line number on handle switch, and increment it manually. See also IO::CatHandle::AutoLines
and LN
modules that simplify this operation.
$INPUT_RECORD_SEPARATOR, $RS, $/§
This is accessed through the .nl-in
method on the filehandle. E. g. $*IN.nl-in
.
$OUTPUT_RECORD_SEPARATOR, $ORS, $\§
This is accessed through the .nl-out
method on the filehandle. E. g. $*OUT.nl-out
.
$OUTPUT_AUTOFLUSH, $|§
No global alternative available. TTY handles are unbuffered by default, for others, set out-buffer to zero or use :!out-buffer
with open on a specific IO::Handle
.
${^LAST_FH}§
Not implemented in Raku.
Variables related to formats§
There are no built-in formats in Raku.
Error variables§
Because of how error variables have changed in Raku, they will not be detailed here individually.
To quote the Raku docs, "$! is the error variable." That's it. All the error variables appear to have been eaten by $!. As with the rest of Raku, it's an object that will return various things depending on the type of error or Exception
.
In particular, when dealing with Exception
s the $!
provides information about the thrown exception, assuming the program has not halted:
try# outside the catch block the exception is placed# into $!say 'outside the catch:';say $!.^name ~ ' : ' ~ $!.message;
and the above code produces the following output
within the catch:
X::AdHoc : Boooh
outside the catch:
X::AdHoc : Boooh
therefore, as stated before, the $!
variable holds the exception object.
Variables related to the interpreter state§
$COMPILING, $^C, $^D, ${^ENCODING}, ${^GLOBAL_PHASE}§
$^H, %^H, ${^OPEN}§
$PERLDB, $^P§
${^TAINT}§
${^UNICODE}, ${^UTF8CACHE}, ${^UTF8LOCALE}§
Not implemented in Raku.