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

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.

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).

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.

$$ is replaced in Perl 6 by $*PID

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).

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.Numeric, while the group name via $*GROUP.Str.

The effective group id does not appear to be currently provided by Perl 6.

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.Numeric, while the username via $*USER.Str.

The effective user id does not appear to be currently provided by Perl 6.

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 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 { $^a cmp $^z }156423;
# OUTPUT: «(1 2 3 4 5 6)␤» 
sort { $^g cmp $^a }156423;
# OUTPUT: «(6 5 4 3 2 1)␤» 
for 1..9 { say $^c$^a$^blast }
# OUTPUT: «312␤» 

For more on placeholder variables, see this page

%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.

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.Str, which returns something like Perl 6 (6.c).

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 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.

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.repo-chain;

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.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":

All the above objects have methods in common:

As a short example, the following piece of code prints information about all the above components:

for $*KERNEL$*DISTRO$*VM -> $what {
    say $what.^name;
    say 'version '  ~ $what.version
        ~ ' named ' ~ $what.name
        ~ ' by '    ~ $what.auth;
}
 
# 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.

[NEEDS FURTHER RESEARCH] No equivalent variable. S28 indicates that this functionality is dealt with in Perl 6 by event filters and exception translation.

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.

As with $] this has been replaced with $*PERL.version.

There is no analog to this in Perl 6.

This has been replaced by $*EXECUTABLE-NAME. Note that there is also $*EXECUTABLE, which is an IO object in Perl 6.

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.

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.

$/ 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.

Since the former performance issues are done away with, this variable is not of use in Perl 6.

Replaced by $/.prematch.

Since the former performance issues are done away with, this variable is not of use in Perl 6.

Replaced by $/.postmatch.

Since the former performance issues are done away with, this variable is not of use in Perl 6.

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.

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 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.

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 5'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.filename.

@*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::Handle types, you can zip with a Range:

for 1..* Z "foo".IO.lines -> ($ln$text{
    say "$ln$text"
}
# 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 Perl 6.

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 {
    fail "Boooh";
    CATCH {
        # within the catch block 
        # the exception is placed into $_ 
        say 'within the catch:';
        say $_.^name ~ ' : ' ~ $_.message;
        $_.resume# do not abort 
    }
}
 
# 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.

Currently no equivalents for either of these variables.

Although deprecated in Perl 5, this may have some sort of equivalent in $?ENC, but this is far from clear.

No Perl 6 equivalent.

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...

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.

S28 claims this variable is "pending". Not currently in Perl 6.

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.

Deprecated and removed variables

It should go without saying that, as these have been removed from Perl 5 already, there should be no need to tell you how to use them in Perl 6.