Perl 5 to Perl 6 guide - special variables
A comparison of special variables in Perl 5 and Perl 6
DESCRIPTION
A (hopefully) comprehensive list of Perl 5 Special Variables with their Perl 6 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 5 to their equivalents in Perl 6. For full documentation on the Perl 6 Special Variables, please see the Perl 6 documentation for each of them.
SPECIAL VARIABLES
General variables
$ARG, $_
Thankfully, $_
is the general default variable as in Perl 5. The main difference in Perl 6 is that you can now call methods on it. For instance, Perl 5's say $_
can be rendered in Perl 6 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 Perl 6 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 5. 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 Perl 6. Design document S28 says there isn't one, so you probably don't want to hold your breath.
$PROCESS_ID, $PID, $$
$$
is replaced in Perl 6 by $*PID
$PROGRAM_NAME, $0
You can access the program name in Perl 6 via $*PROGRAM-NAME
.
Note: $0
in Perl 6 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 Perl 6 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 Perl 6.
$REAL_USER_ID, $UID, $<
In Perl 6 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 does not appear to be currently provided by Perl 6.
$SUBSCRIPT_SEPARATOR, $SUBSEP, $;
The subscript separator variable is not included in Perl 6. Frankly, if your Perl 5 code is using this, it's almost certainly really, really old.
$a, $b
$a
and $b
have no special meaning in Perl 6. 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 Perl 6. Note that the keys of this hash may not be exactly the same between Perl 5 and Perl 6. For example, OLDPWD
is missing from Perl 6's %ENV.
$OLD_PERL_VERSION, $]
The running version of Perl 6 is kept by $*PERL
special variable, that is an object. The running version is retrieved via $*PERL.version
, which returns something like v6.c
; the full stringified version of the Perl interpreter is obtained via ~$*PERL
, which returns something like Perl 6 (6.c)
.
$SYSTEM_FD_MAX, $^F
Although the design documents (S28) indicate that this will likely become $*SYS_FD_MAX
, this has not yet been implemented.
@F
[NEEDS FURTHER RESEARCH] A bit confusing at this point. Design doc S28 indicates that @F
in Perl 5 is replaced by @_
in Perl 6, but it's unclear just how that works. On the other hand, it's currently something of a moot point, as the Perl 5 to Perl 6 Translation doc indicates that the -a
and -F
command-line switches are not yet implemented in rakudo.
@INC
No longer exists in Perl 6. 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 Perl 6.
# Print out a list of compunit repositories.say for .repo-chain;
%INC
No longer exists in Perl 6. 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 Perl 6.
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.
= 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
$BASETIME, $^T
Replaced in Perl 6 by $*INIT-INSTANT
. Unlike in Perl 5, 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 $*PERL.version
.
${^WIN32_SLOPPY_STAT}
There is no analog to this in Perl 6.
$EXECUTABLE_NAME, $^X
This has been replaced by $*EXECUTABLE-NAME
. Note that there is also $*EXECUTABLE
, which is an IO
object in Perl 6.
Variables related to regular expressions
Performance issues
As shown below, $`
, $&
, and $'
are gone from Perl 6, primarily replaced by variations on $/
and, with their elimination, the associated performance issues in Perl 5 do not apply.
$<digits> ($1, $2, ...)
These existing variables do the same thing in Perl 6 as they do in Perl 5, 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 5 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 Perl 6.
$PREMATCH, $`
Replaced by $/.prematch
.
${^PREMATCH}
Since the former performance issues are done away with, this variable is not of use in Perl 6.
$POSTMATCH, $'
Replaced by $/.postmatch
.
${^POSTMATCH}
Since the former performance issues are done away with, this variable is not of use in Perl 6.
$LAST_PAREN_MATCH, $+
Does not exist in Perl 6, 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.
$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 Perl 6. 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 5 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 5'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 Perl 6.
Variables related to formats
There are no built-in formats in Perl 6.
Error variables
Because of how error variables have changed in Perl 6, they will not be detailed here individually.
To quote the Perl 6 docs, "$! is the error variable." That's it. All the error variables appear to have been eaten by $!. As with the rest of Perl 6, it's an object that will return various things depending on the type of error or exception.
In particular, when dealing with exceptions 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.
Variables related to the interpreter state
$COMPILING, $^C, $^D, ${^ENCODING}, ${^GLOBAL_PHASE}
Currently no equivalents for either of these variables.
$^H, %^H, ${^OPEN}
There may or may not be equivalents of these in Perl 6, but they're internal and you shouldn't be messing with them in the first place - certainly not if your understanding of Perl 6 requires you to read this document...
$PERLDB, $^P
The chance of the Perl 6 debugger resembling the Perl 5 debugger is slim at best, and at this point there does not seem to be an equivalent of this variable.
${^TAINT}
S28 claims this variable is "pending". Not currently in Perl 6.
${^UNICODE}, ${^UTF8CACHE}, ${^UTF8LOCALE}
These Unicode-related variables do not appear to exist in Perl 6, but - maybe? - could have analogs in $?ENC
somewhere. This, however, is totally unconfirmed.