=begin pod

=TITLE 5to6-perlvar

=SUBTITLE Perl 5 to Perl 6 guide - special variables

=head1 DESCRIPTION

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

=head1 NOTE

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

=head1 SOURCES

I'm taking my information from L<http://doc.perl6.org/language/variables>
supplemented by L<http://design.perl6.org/S28.html> and input from the
denizens of #perl6 on irc.freenode.org

=head1 SPECIAL VARIABLES

=head2 General Variables

=item $ARG

=item $_


Thankfully, C<$_> is the general default variable as in Perl 5. The
main difference in Perl 6 seems to be that you can now call methods on
it. For instance, Perl 5's C<say $_> can be rendered in Perl 6 as
C<$_.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 C<.say>.

=item @ARG

=item @_

As Perl 6 now has function signatures, your arguments can go there, rather
than depending on C<@_> for them. In fact, if you use a function signature, use
of C<@_> will spit at you telling it cannot override an existing signature.

If, however, you do not use a function signature, C<@_> will contain the
arguments you pass to the function as it did in Perl 5. Again, as with C<$_>,
you can call methods on it. Unlike C<$_> you cannot assume C<@_> as the
default variable for those methods to operate on. I. e. C<@_.shift> works,
C<.shift> does not.

=item $LIST_SEPARATOR

=item $"

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.

=item $PROCESS_ID

=item $PID

=item $$

C<$$> is replaced in Perl 6 by C<$*PID>

=item $PROGRAM_NAME
=item $0

You can access the program name in Perl 6 via C<$*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).

=item $REAL_GROUP_ID

=item $GID

=item $(

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

=item $EFFECTIVE_GROUP_ID

=item $EGID

=item $)

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

=item $REAL_USER_ID

=item $UID

=item $<

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

=item $EFFECTIVE_USER_ID

=item $EUID

=item $>

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

=item $SUBSCRIPT_SEPARATOR

=item $SUBSEP

=item $;

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.

=item $a

=item $b

C<$a> and C<$b> have no special meaning in Perl 6. C<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 C<^> twigil (e. g. C<$^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
C<($^q, $^z, $^a)>, they will be I<assigned> in the order C<($^a, $^q,
$^z)>. Ergo:

=begin code

    sort { $^a cmp $^z }, 1,5,6,4,2,3;
    # 1,2,3,4,5,6
    sort { $^g cmp $^a }, 1,5,6,4,2,3;
    # 6,5,4,3,2,1
    for 1..9 { say $^c, $^a, $^b; last }
    # 312

=end code

For more on placeholder variables, see
L<http://doc.perl6.org/language/variables#The_%5E_Twigil>
=item %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. As of this writing, the
only difference seems to be that OLDPWD is missing from Perl 6's %ENV.

=item $OLD_PERL_VERSION

=item $]

The version of perl is returned by C<$*PERL.version>. For the beta this was
"v6.b" with C<$*PERL> containing "Perl 6 (6.b)".

=item $SYSTEM_FD_MAX

=item $^F

Although the design documents (S28) indicate that this will likely
become C<$*SYS_FD_MAX>, this has not yet been implemented.

=item @F

[NEEDS FURTHER RESEARCH] A bit confusing at this point. Design doc S28
indicates that C<@F> in Perl 5 is replaced by C<@_> 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 C<-a>
and <-F> command-line switches are not yet implemented in rakudo.

=item @INC

[NEEDS FURTHER RESEARCH] If you've been following along, it will
probably not surprise you that this is C<@*INC> in Perl 6. The Perl 6
docs say "Each repository is represented as an object with the
CompUnitRepo interface in the @*INC array" which could probably use some
further explanation. With the version of rakudo star I'm working with,
each directory is prefixed with either C<file#> or C<inst#>. Presumably
this will become clear when documentation for CompUnitRepo shows up.

=item %INC

[NEEDS FURTHER RESEARCH] No longer exists. S28 says it is "encapsulated
in CompUnitRepo object" which, as with C<@INC> raises the question of
what that means.

=item $INPLACE_EDIT

=item $^I

S28 suggests $*INPLACE_EDIT, but it does not yet seem to exist.

=item $^M

S28 suggests $*EMERGENCY_MEMORY, but it does not yet seem to exist.

=item $OSNAME

=item $^O

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

    C<$*KERNEL> gives "darwin (14.3.0)"
    C<$*DISTRO> gives "macosx (10.10.3)"

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

S28 also lists C<$*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.

=item %SIG

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

=item $BASETIME

=item $^T

Replaced in Perl 6 by C<$*INITITME>. Unlike in Perl 5, this is not in seconds
since epoch, but an C<Instant> object, which is measured in atomic seconds,
with fractions.

=item $PERL_VERSION

=item $^V

As with C<$]> this has been replaced with C<$PERL.version>.

=item ${^WIN32_SLOPPY_STAT}

There does not seem to be any analog to this in Perl 6.

=item $EXECUTABLE_NAME

=item $^X

This has been replaced by C<$*EXECUTABLE-NAME>. Confusingly, the Perl 6
Variables doc says "Favor $*EXECUTABLE because it is not guaranteed that
the perl executable is in PATH." Confusingly because C<$*EXECUTABLE> is
an C<IO> object, which does not seem to be what you would want here.
C<$*EXECUTABLE.Str> would give the desired result, though.

=head2 Variables related to regular expressions

=head3 Performance issues

As shown below, C<$`>, C<$&>, and C<$'> are gone from Perl 6, primarily
replaced by variations on C<$/> and, with their elimination, the
associated performance issues in Perl 5 do not apply.

=item $<I<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 C<$0> rather than C<$1>. Furthermore, they are
synonyms for indexed items in the match variable C<$/>. I. e. C<$0> is equivalent
to C<$/[0]>, C<$1> is equivalent to C<$/[1]>, etc.

=item $MATCH

=item $&

C<$/> now contains the match object, so the Perl 5 behavior of C<$&> can
be obtained by stringifying it, i. e. C<~$/>. C<$/.Str> also should
work, but C<~$/> seems to be the consensus preference.

=item ${^MATCH}

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

=item $PREMATCH

=item $`

Replaced by C<$/.prematch>.

=item ${^PREMATCH}

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

=item $POSTMATCH

=item $'

Replaced by C<$/.postmatch>.

=item ${^POSTMATCH}

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

=item $LAST_PAREN_MATCH

=item $+

Does not exist in Perl 6, but you can get the same information using C<$/[*-
1].Str> (C<$/[*-1]> would be the match object, not the actual string).

If you want to I<understand> why that works, you can look at these documents:

=item L<http://doc.perl6.org/routine/%5B%20%5D#language_documentation_Operators>

=item L<http://doc.perl6.org/type/Whatever>

...and possibly

=item L<http://design.perl6.org/S02.html#line_1126>

...though the design documents are not always up to date.

=item $LAST_SUBMATCH_RESULT

=item $^N

S28 suggests C<$*MOST_RECENT_CAPTURED_MATCH>, but there does not seem to be
any implemented variable that matches C<$^N>.

=item @LAST_MATCH_END

=item @+

As with most regular expression related variables, this functionality
is, at least in part, moved to the C<$/> 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 C<.to> method. I. e. the first offset is
C<$/[0].to>, which is synonymous with C<$0.to>. The value Perl 5
provides as C<$+[0]> is provided by C<$/.to>.

=item %LAST_PAREN_MATCH

=item %+

Once again, we move over to C<$/>. The former C<$+{$match}> is
C<$/{$match}>.

=item @LAST_MATCH_START

=item @-

Similarly to C<@+> being replaced by using the C<.to> method, C<@-> is
replaced by using the C<.from> method on C<$/> and its variations. The
first offset is C<$/[0].from> or the equivalent C<$0.from>. Perl 5's C<$-
[0]> is C<$/.from>.


=item %LAST_MATCH_START

=item %-

Much like C<%+>, a use of C<%-{$match}> would be replaced with C<$/{$match}>.

=item $LAST_REGEXP_CODE_RESULT

=item $^R

S28 suggests C<$*LAST_REGEXP_CODE_RESULT>, but no variable seems to actually
exist yet.

=item ${^RE_DEBUG_FLAGS}

No equivalent.

=item ${^RE_TRIE_MAXBUF}

No equivalent.

=head2 Variables related to filehandles

=item $ARGV

The name of the current file when reading lines can be obtained through
C<$*ARGFILES.filename>.

=item @ARGV

C<@*ARGS> contains the command line arguments.

=item ARGV

This has been replaced by C<$*ARGFILES>.

=item ARGVOUT

As the C<-i> command line switch has not yet been implemented, there is not
yet an equivalent of C<ARGVOUT>.

=item $OUTPUT_FIELD_SEPARATOR

=item $OFS

=item $,

Currently no obvious equivalent.

=item $INPUT_LINE_NUMBER

=item $NR

=item $.

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

=item $INPUT_RECORD_SEPARATOR

=item $RS

=item $/

This is accessed through the C<.nl-in> method on the filehandle. E. g.
C<$*IN.nl-in>.

=item $OUTPUT_RECORD_SEPARATOR

=item $ORS

=item $\

This is accessed through the C<.nl-out> method on the filehandle. E. g.
C<$*OUT.nl-out>.

=item $OUTPUT_AUTOFLUSH

=item $|

Currently autoflush is not implemented in Perl 6.

=item ${^LAST_FH}

Not implemented in Perl 6.

=head3 Variables related to formats

There are no built-in formats in Perl 6.

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

=head2 Variables related to the interpreter state

=item $COMPILING

=item $^C

=item $^D

Currently no equivalents for either of these variables.

=item ${^ENCODING}

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

=item ${^GLOBAL_PHASE}

No Perl 6 equivalent.

=item $^H

=item %^H

=item ${^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...

=item $PERLDB

=item $^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.

=item ${^TAINT}

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

=item ${^UNICODE}

=item ${^UTF8CACHE}

=item ${^UTF8LOCALE}

These Unicode-related variables do not appear to exist in Perl 6, but -
maybe? - could have analogs in C<$?ENC> somewhere. This, however, is
totally unconfirmed.

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

=end pod
