Perl 5 to Perl 6 guide - special variables


A (hopefully) comprehensive list of Perl 5 Special Variables with their Perl 6 equivalents with notes on variations between them where necessary.


I will not be explaining the full use of the Perl 6 variables. This document is an attempt to guide you 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 those 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 match (i. e. capture variables now start from $0 rather than $1).

The real group id is provided by $*GROUP.Numeric in Perl 6. $*GROUP.Str returns the group name, rather than its number.

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

The real user id is provided by $*USER.Numeric in Perl 6. $*USER.Str returns the user name, rather than its number.

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;
    # (1 2 3 4 5 6) 
    sort { $^g cmp $^a }156423;
    # (6 5 4 3 2 1) 
    for 1..9 { say $^c$^a$^blast }
    # 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 version of perl is returned by $*PERL.version. For the beta this was "v6.b" with $*PERL containing "Perl 6 (6.b)".

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 S28 has three different suggestions, all of which give different answers. On my OS X machine at this time...

say $*KERNEL# gives "darwin (14.3.0)" 
say $*DISTRO# gives "macosx (10.10.3)" 

Using .version on either of those will give you just the version number. .name gives the kernel or distro name. Those objects contain other information as well.

S28 also lists $*VM (which with my build of rakudo star currently gives "moar (2015.5.63.ge.7.a.473.c)"), but I'm unclear how the VM relates at all to the operating system.

[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 $*INITTIME. 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 this is an IO object in Perl 6, so use ~$*EXECUTABLE-NAME to get a Str to be closer to Perl 5.

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. ~$/. $/.Str also should work, but ~$/ is the currently 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.

The current line number is returned by the .ins method on the appropriate filehandle. E. g. $*IN.ins.

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.

Currently autoflush is not implemented in Perl 6.

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, I will not detail the changes 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 likely an object that will return various things depending on how you use it. Sadly, at the moment, the documentation for it is terribly sparse. It will probably do what you intend, but I'm not guaranteeing anything. Hopefully there will be more information in the near future.

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.