Skip to content

Commit

Permalink
Several improvements on this page
Browse files Browse the repository at this point in the history
It actually started with #1566, trying to improve the description of
constants. However, while I was at it, I noticed references to
Synopsis, so I changed a few of them working on #2225.
This also fixes #2226 and, in fact, has generated rakudo/rakudo#2141
for a command line flag that was referenced here and was actually
working on Perl 6 (undocumented). Of course, there was a bit of #2223
and a bit of reflow.

I guess now it's back to work on #1566 or maybe #2225.
  • Loading branch information
JJ committed Jul 28, 2018
1 parent b75421d commit 176e2e2
Showing 1 changed file with 64 additions and 52 deletions.
116 changes: 64 additions & 52 deletions doc/Language/5to6-nutshell.pod6
@@ -1,6 +1,6 @@
=begin pod :tag<convert>
=TITLE Perl 5 to Perl 6 guide - In a Nutshell
=TITLE Perl 5 to Perl 6 guide - In a nutshell
=SUBTITLE How do I do what I used to do? (Perl 6 in a nutshell)
Expand Down Expand Up @@ -63,9 +63,13 @@ all at once.
=head1 Syntax
There are a few differences in syntax between the two languages, starting with
how identifiers are defined.
=head2 Identifiers
In identifiers, Perl 6 allows the use of dashes (C<->), underscores (C<_>), apostrophes (C<'>), and alphanumerics:
Perl 6 allows the use of dashes (C<->), underscores (C<_>),
apostrophes (C<'>), and alphanumerics in identifiers, :
sub test-doesn't-hang { ... }
my $ความสงบ = 42;
Expand Down Expand Up @@ -463,27 +467,30 @@ my @array = 4,8,15;
say @array; # OUTPUT: «[66 8 15]␤»
=end code
The underlying Array object of C<@array> is passed, and its first value modified inside the declared routine.
The underlying Array object of C<@array> is passed, and its first value modified
inside the declared routine.
In Perl 5, the syntax for dereferencing an entire reference is the
type-sigil and curly braces, with the reference inside the curly braces. In Perl 6, this concept simply does not apply, since the I<reference> metaphor does not really apply.
In Perl 5, the syntax for dereferencing an entire reference is the type-sigil
and curly braces, with the reference inside the curly braces. In Perl 6, this
concept simply does not apply, since the I<reference> metaphor does not really
apply.
In Perl 5, the arrow operator, C«->» , is used for single access to a
composite's reference or to call a sub through its reference. In Perl 6,
the dot operator C<.> is always used for object methods, but the rest does not really apply.
composite's reference or to call a sub through its reference. In Perl 6, the dot
operator C<.> is always used for object methods, but the rest does not really
apply.
=for code :lang<perl5>
# Perl 5
say $arrayref->[7];
say $hashref->{'fire bad'};
say $subref->($foo, $bar);
In relatively recent versions of Perl 5 (5.20 and later), a new feature allows the use of the arrow
operator for dereferencing: see
L<Postfix Dereferencing|http://search.cpan.org/~shay/perl-5.20.1/pod/perl5200delta.pod#Experimental_Postfix_Dereferencing>. This
can be used to create an array from a scalar. This operation is
usually called I<decont>, as in decontainerization, and in Perl 6
methods such as C<.list> and C<.hash> are used:
In relatively recent versions of Perl 5 (5.20 and later), a new feature allows
the use of the arrow operator for dereferencing: see L<Postfix Dereferencing|http://search.cpan.org/~shay/perl-5.20.1/pod/perl5200delta.pod#Experimental_Postfix_Dereferencing>.
This can be used to create an array from a scalar. This operation is usually
called I<decont>, as in decontainerization, and in Perl 6 methods such as
C<.list> and C<.hash> are used:
=for code :lang<perl5>
# Perl 5.20
Expand All @@ -504,11 +511,12 @@ The "Zen" slice does the same thing:
my @a = $contains-an-array[];
my %h = $contains-a-hash{};
See L<the "Containers" section of the documentation|/language/containers> for more information.
See L<the "Containers" section of the documentation|/language/containers> for
more information.
=head1 Operators
See L<S03/Operators|https://design.perl6.org/S03.html#Overview>
See L<the documentation for operators|/language/operators>
for full details on all operators.
Unchanged:
Expand Down Expand Up @@ -559,7 +567,7 @@ type of its arguments.
While the operator has not changed, the rules for what exactly is matched
depend on the types of both arguments, and those rules are far from
identical in Perl 5 and Perl 6. See L<~~|/routine/~~> and
L<S03/Smartmatching|https://design.perl6.org/S03.html#Smart_matching>
L<the smartmatch operator|/language/operators#index-entry-smartmatch_operator>
=head2 C<& | ^> String Bitwise ops
=head2 C<& | ^> Numeric Bitwise ops
Expand Down Expand Up @@ -708,19 +716,22 @@ operators. They have been replaced by C<ff> and C<fff>.
=head2 String interpolation
In Perl 5, C<"${foo}s"> deliminates a variable name from regular text next to it.
In Perl 6, simply extend the curly braces to include the sigil too: C<"{$foo}s">.
This is in fact a very simple case of interpolating an expression.
In Perl 5, C<"${foo}s"> deliminates a variable name from regular text next to
it. In Perl 6, simply extend the curly braces to include the sigil too:
C<"{$foo}s">. This is in fact a very simple case of interpolating an expression.
=head1 Compound Statements
These statements include conditionals and loops.
=head2 Conditionals
=head3 C<if> C<elsif> C<else> C<unless>
Mostly unchanged; parentheses around the conditions are now optional, but if
used, must not immediately follow the keyword, or it will be taken as a function
call instead. Binding the conditional expression to a variable is also a little different:
call instead. Binding the conditional expression to a variable is also a little
different:
=for code :lang<perl5>
if (my $x = dostuff()) {...} # Perl 5
Expand Down Expand Up @@ -774,7 +785,8 @@ See also the warnings on the smartmatch op above.
Mostly unchanged; parentheses around the conditions are now optional, but if
used, must not immediately follow the keyword, or it will be taken as a function
call instead. Binding the conditional expression to a variable is also a little different:
call instead. Binding the conditional expression to a variable is also a little
different:
=for code :lang<perl5>
while (my $x = dostuff()) {...} # Perl 5
Expand Down Expand Up @@ -830,7 +842,7 @@ repeat {
=head3 C<for> C<foreach>
Note first this common misunderstanding about the C<for> and C<foreach>
keywords. Many programmers think that they distinguish between the C-style
keywords: Many programmers think that they distinguish between the C-style
three-expression form and the list-iterator form; they do not! In fact,
the keywords are interchangeable; the Perl 5 compiler looks for the
semi-colons within the parentheses to determine which type of loop to parse.
Expand Down Expand Up @@ -1019,8 +1031,7 @@ $line =~ s/abc/123/; # Perl 5
$line ~~ s/abc/123/; # Perl 6
Alternately, the new C<.match> and C<.subst> methods can be used. Note that
C<.subst> is non-mutating. See
L<S05/Substitution|https://design.perl6.org/S05.html#Substitution>.
L<C<.subst> is non-mutating|/routine/subst>.
=head2 Captures start with 0, not 1
Expand Down Expand Up @@ -1161,8 +1172,7 @@ else {
# gracefully handle the fact that the open() failed
}
=head3 C<base>
=head3 C<parent>
=head3 C<base>, C<parent>
Both C<use base> and C<use parent> have been replaced in Perl 6 by the
C<is> keyword, in the class declaration.
Expand All @@ -1188,11 +1198,11 @@ denominator is limited to C<2**64>, after which it will automatically
upgrade to C<Num> to preserve performance). If you want a C<Rat> with
an arbitrary-precision denominator, C<FatRat> is available.
=head3 X<C<constant>|constant>
=head3 C<constant>
In Perl 6, C<constant> is a declarator for variables, just like C<my>,
except the variable is permanently locked to the result of its
initialization expression (evaluated at compile time).
In Perl 6, C<constant> is a declarator for variables, just like C<my>, except
the variable is permanently locked to the result of its initialization
expression (evaluated at compile time).
So, change the C«=>» to C<=>.
Expand All @@ -1209,7 +1219,7 @@ use constant pi => 4 * atan2(1, 1); # Perl 5
=head3 C<encoding>
TODO Allows you to write your script in non-ascii or non-utf8
TODO Allows you to write your script in non-ascii or non-utf8.
=head3 C<integer>
Expand Down Expand Up @@ -1241,9 +1251,7 @@ say Animal.^mro; # .^ indicates calling a meta-method on the object
=head3 C<utf8>
No longer relevant.
In Perl 6, source code is expected to be in utf8 encoding.
No longer relevant: in Perl 6, source code is expected to be in utf8 encoding.
=head3 C<vars>
Expand All @@ -1257,11 +1265,11 @@ before translating into Perl 6.
=head1 Command-line flags
See
L<S19/commandline|https://design.perl6.org/S19.html#Command_Line_Elements>
L<the command line flags that Rakudo uses|https://github.com/rakudo/rakudo/wiki/Running-rakudo-from-the-command-line>
Unchanged:
-c -e -h -I -n -p -S -T -v -V
-c -e -h -I -n -p -v -V
=head3 C<-a>
Expand Down Expand Up @@ -1323,6 +1331,10 @@ Removed. See L<S19#Removed Syntactic Features|https://design.perl6.org/S19.html#
This is now the default behavior.
=item C<-S>, C<-T>.
This has been eliminated. Several ways to L<replicate "taint" mode are discussed in Reddit|https://www.reddit.com/r/perl6/comments/718z4o/taint_mode_for_perl_6/>.
=head1 File-related operations
=head2 Reading the lines of a text file into an array
Expand Down Expand Up @@ -1410,8 +1422,8 @@ probably guessed, you just need to use X<C<PERL6LIB>|PERL6LIB>:
=for code :lang<shell>
$ PERL6LIB="/some/module/lib" perl6 program.p6
In Perl 5 one uses the ':' (colon) as a directory separator for C<PERL5LIB>, but in
Perl 6 one uses the ',' (comma). For example:
In Perl 5 one uses the ':' (colon) as a directory separator for C<PERL5LIB>, but
in Perl 6 one uses the ',' (comma). For example:
=for code :lang<shell>
$ export PERL5LIB=/module/dir1:/module/dir2;
Expand Down Expand Up @@ -1499,9 +1511,11 @@ If one tries to use C<baz> an "Undeclared routine" error is raised at compile ti
So, how does one recreate the Perl 5 behaviour of being able to selectively
import functions? For this one needs to define an C<EXPORT> sub inside the
module which specifies the functions to be exported and remove the C<module Bar> statement.
module which specifies the functions to be exported and remove the C<module Bar>
statement.
The module C<Bar> now is merely a file called C<Bar.pm> with the following contents:
The module C<Bar> now is merely a file called C<Bar.pm> with the following
contents:
=begin code :preamble<no strict;>
use v6.c;
Expand Down Expand Up @@ -1719,9 +1733,7 @@ Note that Perl 6 auto-generates a full usage message on error in
command-line parsing.
=head1 Automated Translation
=head1 Automated translation
A quick way to find the Perl 6 version of a Perl 5 construct, is to run it
through an automated translator.
Expand All @@ -1739,7 +1751,7 @@ L<https://github.com/Util/Blue_Tiger/>
=head2 Perlito
Online Translator!
Online translator!
This project is a suite of Perl cross-compilers, including Perl 5-to-6
translation. It has a web front-end, and so can be used without
Expand All @@ -1749,14 +1761,14 @@ L<http://fglock.github.io/Perlito/perlito/perlito5.html>
=head2 Perl-ToPerl6
Jeff Goff's Perl::ToPerl6 module for Perl 5 is designed around
Perl::Critic's framework. It aims to convert Perl5 to compilable (if not
necessarily running) Perl 6 code with the bare minimum of changes. Code
transformers are configurable and pluggable, so you can create and
contribute your own transformers, and customize existing transformers to
your own needs. You can install the latest release from CPAN, or follow
the project live on GitHub. An online converter may become available at
some point.
Jeff Goff's
L<Perl::ToPerl6|https://metacpan.org/release/JGOFF/Perl-ToPerl6-0.03> module for
Perl 5 is designed around Perl::Critic's framework. It aims to convert Perl5 to
compilable (if not necessarily running) Perl 6 code with the bare minimum of
changes. Code transformers are configurable and pluggable, so you can create and
contribute your own transformers, and customize existing transformers to your
own needs. You can install the latest release from CPAN, or follow the project
live on GitHub. An online converter may become available at some point.
=head1 Other sources of translation knowledge
Expand Down

0 comments on commit 176e2e2

Please sign in to comment.