Skip to content

Commit 88b116e

Browse files
authored
Merge pull request #2504 from uzluisf/master
Whitespace and minor improvements. Refs #2354
2 parents c14b44c + e4d0c13 commit 88b116e

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

doc/Language/py-nutshell.pod6

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
=SUBTITLE Learning Perl 6 from Python, in a nutshell
66
77
This page is an attempt to provide a way to learn Perl 6
8-
for folks coming from a Python background. We discuss
8+
for folks coming from a Python background. We discuss
99
the equivalent syntax in Perl 6 for a number of Python
1010
constructs and idioms.
1111
1212
=head1 Basic syntax
1313
1414
=head2 Hello, world
1515
16-
Let's start with printing "Hello, world!". L<put> in Perl 6 is the
17-
equivalent of L<print> in Python. Like Python 2, parentheses are
18-
optional. A return is added to the end of the line.
16+
Let's start with printing "Hello, world!". The L<put> keyword in Perl 6
17+
is the equivalent of L<print> in Python. Like Python 2, parentheses are
18+
optional. A newline is added to the end of the line.
1919
2020
Python 2
2121
@@ -31,14 +31,14 @@ Perl 6
3131
3232
put "Hello, world!"
3333
34-
There is also L<say>, which behaves similarly, but will call
34+
There is also the L<say> keyword, which behaves similarly, but will call
3535
the L<gist> method of its argument.
3636
3737
Perl 6
3838
39-
my $hello; ...;
40-
say "Hello, world!"; # also prints "Hello, world"
41-
say $hello; # same as: put $hello.gist
39+
my $hello = "Hello, world!";
40+
say $hello; # also prints "Hello, world!"
41+
# same as: put $hello.gist
4242
4343
In Python, C<'> and C<"> are interchangeable.
4444
In Perl 6, both may be used for quoting, but double
@@ -57,14 +57,14 @@ Perl 6
5757
5858
In Python, a newline signifies the end of a statement.
5959
There are a few exceptions: A backslash before a newline
60-
continues a statement across lines. Also if there is
60+
continues a statement across lines. Also if there is
6161
an unmatched opening parentheses, square bracket, or curly
6262
brace, the statement continues across lines, until the
6363
matching curly braces are closed.
6464
6565
In Perl 6, a semicolon signifies the end of a statement.
6666
The semicolon may be omitted if it is the last statement
67-
of a block. The semicolon may also be omitted if there
67+
of a block. The semicolon may also be omitted if there
6868
is a closing curly brace followed by a newline.
6969
7070
Python
@@ -79,11 +79,12 @@ Perl 6
7979
8080
say 1 + 2 +
8181
3 + 4;
82-
if True { say 1 + 2 }
82+
say 1 +
83+
2;
8384
8485
=head2 Blocks
8586
86-
In Python, indentation is used to indicate a block. Perl 6
87+
In Python, indentation is used to indicate a block. Perl 6
8788
uses curly braces.
8889
8990
Python
@@ -102,7 +103,7 @@ Perl 6
102103
say "1 is not 2."
103104
}
104105
105-
Parentheses are optional in both languages in expressions in
106+
Parentheses are optional in both languages for expressions in
106107
conditionals, as shown above.
107108
108109
=head2 Variables
@@ -113,16 +114,18 @@ In Python, variables are declared and initialized at the same time:
113114
foo = 12
114115
bar = 19
115116
116-
In Perl 6, C<my> declares a lexical variable. A variable can be
117-
initialized with C<=>. i.e. these can be written as two statements or one.
117+
In Perl 6, the C<my> declarator declares a lexical variable. A variable can be
118+
initialized with C<=>. This variable can either be declared first and later
119+
initialized or declared and initialized at once.
120+
118121
119122
my $foo; # declare
120123
$foo = 12; # initialize
121124
my $bar = 19; # both at once
122125
123126
Also, as you may have noticed, variables in Perl 6 usually start with
124-
sigils -- symbols indicating the type of their container. Variables
125-
starting with a C<$> hold scalars. Variables starting with an C<@>
127+
sigils -- symbols indicating the type of their container. Variables
128+
starting with a C<$> hold scalars. Variables starting with an C<@>
126129
hold arrays, and variables starting with a C<%> hold a hash (dict).
127130
Immutable variables can be sigil-less, if they are declared with a C<\>.
128131
@@ -156,8 +159,9 @@ Perl 6
156159
=head2 Scope
157160
158161
In Python, functions and classes create a new scope, but no other
159-
block constructor (e.g. loops, conditionals) creates a scope. In
160-
Python 2, list comprehensions do not create a new scope, but in Python 3, they do.
162+
block constructor (e.g. loops, conditionals) creates a scope. In
163+
Python 2, list comprehensions do not create a new scope, but in
164+
Python 3, they do.
161165
162166
In Perl 6, every block creates a lexical scope.
163167
@@ -223,7 +227,7 @@ Perl 6
223227
my $l = * + 12 # same as above
224228
225229
A C<*> in an expression will become a placeholder for the argument,
226-
and transform the expression into a lambda at compile time. Each
230+
and transform the expression into a lambda at compile time. Each
227231
C<*> in an expression is a separate positional parameter.
228232
229233
See the section below for more constructs regarding subroutines and blocks.
@@ -250,9 +254,9 @@ Perl 6
250254
say squares[4]();
251255
# 4, 16 since each loop iteration has a lexically scoped x,
252256
253-
Note that C<^N> is like C<range(N)>. Similarly,
257+
Note that C<^N> is like C<range(N)>. Similarly,
254258
C<N..^M> works like C<range(N, M)> (a list from N
255-
to M - 1). C<N..M> is a list from N to M. The
259+
to M - 1). The range C<N..M> is a list from N to M. The
256260
C<^> before or after the C<..> indicates that the
257261
beginning or ending endpoint of the list (or both)
258262
should be excluded.
@@ -288,11 +292,11 @@ Perl 6 also has C<for> loops and C<while> loops:
288292
$j += 1
289293
}
290294
291-
(Perl 6 also has a few more looping constructs: C<repeat, until>,
292-
C<repeat, while>, C<until>, and C<loop>.)
295+
(Perl 6 also has a few more looping constructs: C<repeat...until>,
296+
C<repeat...while>, C<until>, and C<loop>.)
293297
294298
C<last> leaves a loop in Perl 6, and is analogous to
295-
C<break> in Python. C<continue> in Python is C<next>
299+
C<break> in Python. C<continue> in Python is C<next>
296300
in Perl 6.
297301
298302
Python
@@ -317,8 +321,8 @@ Using C<if> as a statement modifier (as above) is acceptable
317321
in Perl 6, even outside of a list comprehension.
318322
319323
The C<yield> statement within a C<for> loop in Python, which produces a
320-
C<generator>, is like a C<gather>/C<take> construct in Perl 6. These both print
321-
1, 2, 3.
324+
C<generator>, is like a C<gather>/C<take> construct in Perl 6. These
325+
both print 1, 2, 3.
322326
323327
I<Python>
324328
@@ -446,16 +450,16 @@ Perl 6
446450
my $square = { $^x ** 2 }; # placeholder variable
447451
my $square = { $_ ** 2 }; # topic variable
448452
449-
Placeholder variables are lexicographically ordered to form positional parameters.
450-
i.e. these are the same:
453+
Placeholder variables are lexicographically ordered to form positional
454+
parameters. Thus these are the same:
451455
452456
my $power = { $^x ** $^y };
453457
my $power = -> $x, $y { $x ** $y };
454458
455459
=head2 X<List comprehensions|Python>
456460
457-
Postfix statement modifiers and blocks can be combined to make list
458-
comprehensions.
461+
Postfix statement modifiers and blocks can be combined to
462+
easily create list comprehensions in Perl 6.
459463
460464
Python
461465
@@ -468,7 +472,7 @@ Perl 6
468472
say ( { $^i * 2 } for 3, 9 ); # OUTPUT: «(6 18)␤»
469473
say ( -> \i { i * 2 } for 3, 9 ); # OUTPUT: «(6 18)␤»
470474
471-
Conditionals can be applied, but the C<if> comes first,
475+
Conditionals can be applied, but the C<if> keyword comes first,
472476
unlike in Python where the if comes second.
473477
474478
=for code :lang<python>
@@ -496,7 +500,8 @@ C<grep> (which is like Python's C<filter>) is an alternative.
496500
497501
Here's an example from the Python
498502
L<docs|https://docs.python.org/3/tutorial/classes.html#class-and-instance-variables>.
499-
First, "instance variables", aka attributes in Perl 6:
503+
First let's go over "instance variables" which are known as attributes
504+
in Perl 6:
500505
501506
Python:
502507
@@ -511,8 +516,8 @@ Perl 6:
511516
has $.name;
512517
}
513518
514-
Constructors by default take named arguments in Perl 6,
515-
and use the method C<new>.
519+
For each created class, Perl 6 provides the constructor method C<new>
520+
by default which takes named arguments.
516521
517522
Python:
518523
@@ -524,13 +529,12 @@ print e.name
524529
525530
Perl 6
526531
527-
class Dog {}; ...;
528-
my $d = Dog.new(:name<Fido>);
532+
my $d = Dog.new(:name<Fido>); # or: Dog.new(name => 'Fido')
529533
my $e = Dog.new(:name<Buddy>);
530534
say $d.name;
531535
say $e.name;
532536
533-
Class attributes in Perl 6 can be declared in a few ways. One way
537+
Class attributes in Perl 6 can be declared in a few ways. One way
534538
is to just declare a lexical variable and a method for accessing it.
535539
536540
Python:
@@ -562,7 +566,8 @@ Perl 6:
562566
say $d.name;
563567
say $e.name;
564568
565-
To mutate attributes, in Perl 6 you"ll want to use C<is rw>:
569+
In order to mutate attributes in Perl 6, you must use
570+
the C<is rw> trait on the attributes:
566571
567572
Python:
568573
@@ -611,7 +616,9 @@ Perl 6
611616
my $d = Dog.new;
612617
$d.jump;
613618
614-
Multiple inheritance is possible by using C<is> multiple times, or with C<also>.
619+
Multiple inheritance is possible by using the C<is> trait as many times
620+
as required. Alternatively, it can be used in conjuntion with the
621+
C<also> keyword.
615622
616623
Python
617624
@@ -638,7 +645,7 @@ or
638645
=head2 X<Decorators|Python>
639646
640647
Decorators in Python are a way of wrapping a function
641-
in another one. In Perl 6, this is done with C<wrap>.
648+
in another one. In Perl 6, this is done with C<wrap>.
642649
643650
Python
644651
@@ -728,16 +735,15 @@ run on entering or leaving a block.
728735
729736
=head2 X<C<input>|Python>
730737
731-
In Python 3, the C<input> keyword is used to prompt the user. C<input> can be
732-
provided with an optional argument which is written to standard output without
733-
a trailing newline:
738+
In Python 3, the C<input> keyword is used to prompt the user. This keyword
739+
can be provided with an optional argument which is written to standard output
740+
without a trailing newline:
734741
735742
=begin code :lang<python>
736743
user_input = input("Say hi → ")
737744
print(user_input)
738745
=end code
739746
740-
741747
When prompted, you can enter C<Hi> or any other string, which will be stored
742748
in the C<user_input> variable. This is similar to L<prompt> in Perl 6:
743749

0 commit comments

Comments
 (0)