A comparison of special variables in Perl and Raku
A (hopefully) comprehensive list of Perl Special Variables with their Raku equivalents with notes on variations between them where necessary.
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.
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
.
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).
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.
$$
is replaced in Raku by $*PID
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
).
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
.
The effective group id does not appear to be currently provided by Raku.
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
.
The effective user id does not appear to be currently provided by Raku.
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
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 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.
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)
.
Although the design documents (S28) indicate that this will likely become $*SYS_FD_MAX
, this has not yet been implemented.
[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.
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;
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)
S28 suggests $*INPLACE_EDIT, but it does not yet exist.
S28 suggests $*EMERGENCY_MEMORY, but it does not yet exist.
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.
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.
= sub
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
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.
As with $]
this has been replaced with $*RAKU.version
.
There is no analog to this in Raku.
This has been replaced by $*EXECUTABLE-NAME
. Note that there is also $*EXECUTABLE
, which is an IO
object in Raku.
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.
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.
$/
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.
Since the former performance issues are done away with, this variable is not of use in Raku.
Replaced by $/.prematch
.
Since the former performance issues are done away with, this variable is not of use in Raku.
Replaced by $/.postmatch
.
Since the former performance issues are done away with, this variable is not of use in Raku.
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:
...and possibly
...though the design documents are not always up to date.
S28 suggests $*MOST_RECENT_CAPTURED_MATCH
, but there does not seem to be any implemented variable that matches $^N
.
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
.
Once again, we move over to $/
. The former $+{$match}
is $/{$match}
.
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
.
Much like %+
, a use of %-{$match}
would be replaced with $/{$match}
.
No equivalent.
No equivalent.
No equivalent.
The name of the current file when reading lines can be obtained through $*ARGFILES.path
.
@*ARGS
contains the command line arguments.
This has been replaced by $*ARGFILES
.
As the -i
command line switch has not yet been implemented, there is not yet an equivalent of ARGVOUT
.
Currently no obvious equivalent.
No direct replacement exists.
When iterating using lines method from IO::Path or IO::Path 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.
This is accessed through the .nl-in
method on the filehandle. E. g. $*IN.nl-in
.
This is accessed through the .nl-out
method on the filehandle. E. g. $*OUT.nl-out
.
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.
Not implemented in Raku.
There are no built-in formats in Raku.
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 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 : Booohoutside the catch:X::AdHoc : Boooh
therefore, as stated before, the $!
variable holds the exception object.
Currently no equivalents for either of these variables.
There may or may not be equivalents of these in Raku, but they're internal and you shouldn't be messing with them in the first place - certainly not if your understanding of Raku requires you to read this document...
The chance of the Raku debugger resembling the Perl debugger is slim at best, and at this point there does not seem to be an equivalent of this variable.
S28 claims this variable is "pending". Not currently in Raku.
These Unicode-related variables do not appear to exist in Raku, but - maybe? - could have analogs in $?ENC
somewhere. This, however, is totally unconfirmed.