5
5
= SUBTITLE Learning Perl 6 from Python, in a nutshell
6
6
7
7
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
9
9
the equivalent syntax in Perl 6 for a number of Python
10
10
constructs and idioms.
11
11
12
12
= head1 Basic syntax
13
13
14
14
= head2 Hello, world
15
15
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.
19
19
20
20
Python 2
21
21
@@ -31,14 +31,14 @@ Perl 6
31
31
32
32
put "Hello, world!"
33
33
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
35
35
the L < gist > method of its argument.
36
36
37
37
Perl 6
38
38
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
42
42
43
43
In Python, C < ' > and C < " > are interchangeable.
44
44
In Perl 6, both may be used for quoting, but double
@@ -57,14 +57,14 @@ Perl 6
57
57
58
58
In Python, a newline signifies the end of a statement.
59
59
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
61
61
an unmatched opening parentheses, square bracket, or curly
62
62
brace, the statement continues across lines, until the
63
63
matching curly braces are closed.
64
64
65
65
In Perl 6, a semicolon signifies the end of a statement.
66
66
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
68
68
is a closing curly brace followed by a newline.
69
69
70
70
Python
@@ -79,11 +79,12 @@ Perl 6
79
79
80
80
say 1 + 2 +
81
81
3 + 4;
82
- if True { say 1 + 2 }
82
+ say 1 +
83
+ 2;
83
84
84
85
= head2 Blocks
85
86
86
- In Python, indentation is used to indicate a block. Perl 6
87
+ In Python, indentation is used to indicate a block. Perl 6
87
88
uses curly braces.
88
89
89
90
Python
@@ -102,7 +103,7 @@ Perl 6
102
103
say "1 is not 2."
103
104
}
104
105
105
- Parentheses are optional in both languages in expressions in
106
+ Parentheses are optional in both languages for expressions in
106
107
conditionals, as shown above.
107
108
108
109
= head2 Variables
@@ -113,16 +114,18 @@ In Python, variables are declared and initialized at the same time:
113
114
foo = 12
114
115
bar = 19
115
116
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
+
118
121
119
122
my $foo; # declare
120
123
$foo = 12; # initialize
121
124
my $bar = 19; # both at once
122
125
123
126
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 < @ >
126
129
hold arrays, and variables starting with a C < % > hold a hash (dict).
127
130
Immutable variables can be sigil-less, if they are declared with a C < \ > .
128
131
@@ -156,8 +159,9 @@ Perl 6
156
159
= head2 Scope
157
160
158
161
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.
161
165
162
166
In Perl 6, every block creates a lexical scope.
163
167
@@ -223,7 +227,7 @@ Perl 6
223
227
my $l = * + 12 # same as above
224
228
225
229
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
227
231
C < * > in an expression is a separate positional parameter.
228
232
229
233
See the section below for more constructs regarding subroutines and blocks.
@@ -250,9 +254,9 @@ Perl 6
250
254
say squares[4]();
251
255
# 4, 16 since each loop iteration has a lexically scoped x,
252
256
253
- Note that C < ^N > is like C < range(N) > . Similarly,
257
+ Note that C < ^N > is like C < range(N) > . Similarly,
254
258
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
256
260
C < ^ > before or after the C < .. > indicates that the
257
261
beginning or ending endpoint of the list (or both)
258
262
should be excluded.
@@ -288,11 +292,11 @@ Perl 6 also has C<for> loops and C<while> loops:
288
292
$j += 1
289
293
}
290
294
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 > .)
293
297
294
298
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 >
296
300
in Perl 6.
297
301
298
302
Python
@@ -317,8 +321,8 @@ Using C<if> as a statement modifier (as above) is acceptable
317
321
in Perl 6, even outside of a list comprehension.
318
322
319
323
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.
322
326
323
327
I < Python >
324
328
@@ -446,16 +450,16 @@ Perl 6
446
450
my $square = { $^x ** 2 }; # placeholder variable
447
451
my $square = { $_ ** 2 }; # topic variable
448
452
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:
451
455
452
456
my $power = { $^x ** $^y };
453
457
my $power = -> $x, $y { $x ** $y };
454
458
455
459
= head2 X < List comprehensions|Python >
456
460
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 .
459
463
460
464
Python
461
465
@@ -468,7 +472,7 @@ Perl 6
468
472
say ( { $^i * 2 } for 3, 9 ); # OUTPUT: «(6 18)»
469
473
say ( -> \i { i * 2 } for 3, 9 ); # OUTPUT: «(6 18)»
470
474
471
- Conditionals can be applied, but the C < if > comes first,
475
+ Conditionals can be applied, but the C < if > keyword comes first,
472
476
unlike in Python where the if comes second.
473
477
474
478
= for code :lang<python>
@@ -496,7 +500,8 @@ C<grep> (which is like Python's C<filter>) is an alternative.
496
500
497
501
Here's an example from the Python
498
502
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:
500
505
501
506
Python:
502
507
@@ -511,8 +516,8 @@ Perl 6:
511
516
has $.name;
512
517
}
513
518
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 .
516
521
517
522
Python:
518
523
@@ -524,13 +529,12 @@ print e.name
524
529
525
530
Perl 6
526
531
527
- class Dog {}; ...;
528
- my $d = Dog.new(:name<Fido>);
532
+ my $d = Dog.new(:name<Fido>); # or: Dog.new(name => 'Fido')
529
533
my $e = Dog.new(:name<Buddy>);
530
534
say $d.name;
531
535
say $e.name;
532
536
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
534
538
is to just declare a lexical variable and a method for accessing it.
535
539
536
540
Python:
@@ -562,7 +566,8 @@ Perl 6:
562
566
say $d.name;
563
567
say $e.name;
564
568
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:
566
571
567
572
Python:
568
573
@@ -611,7 +616,9 @@ Perl 6
611
616
my $d = Dog.new;
612
617
$d.jump;
613
618
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.
615
622
616
623
Python
617
624
638
645
= head2 X < Decorators|Python >
639
646
640
647
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 > .
642
649
643
650
Python
644
651
@@ -728,16 +735,15 @@ run on entering or leaving a block.
728
735
729
736
= head2 X < C < input > |Python>
730
737
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:
734
741
735
742
= begin code :lang<python>
736
743
user_input = input("Say hi → ")
737
744
print(user_input)
738
745
= end code
739
746
740
-
741
747
When prompted, you can enter C < Hi > or any other string, which will be stored
742
748
in the C < user_input > variable. This is similar to L < prompt > in Perl 6:
743
749
0 commit comments