@@ -19,10 +19,12 @@ optional. A return is added to the end of the line.
19
19
20
20
Python 2
21
21
22
+ = for code :lang<python>
22
23
print "Hello, world!"
23
24
24
25
Python 3
25
26
27
+ = for code :lang<python>
26
28
print("Hello, world!")
27
29
28
30
Perl 6
@@ -34,6 +36,7 @@ the C<gist> method of its argument.
34
36
35
37
Perl 6
36
38
39
+ my $hello; ...;
37
40
say "Hello, world!"; # also prints "Hello, world"
38
41
say $hello; # same as: put $hello.gist
39
42
@@ -66,6 +69,7 @@ is a closing curly brace followed by a newline.
66
69
67
70
Python
68
71
72
+ = for code :lang<python>
69
73
print 1 + 2 + \
70
74
3 + 4
71
75
print ( 1 +
@@ -84,6 +88,7 @@ uses curly braces.
84
88
85
89
Python
86
90
91
+ = for code :lang<python>
87
92
if 1 == 2:
88
93
print "Wait, what?"
89
94
else:
@@ -104,6 +109,7 @@ conditionals, as shown above.
104
109
105
110
In Python, variables are declared and initialized at the same time:
106
111
112
+ = for code :lang<python>
107
113
foo = 12
108
114
bar = 19
109
115
@@ -122,6 +128,7 @@ Immutable variables can be sigil-less, if they are declared with a C<\>.
122
128
123
129
Python
124
130
131
+ = begin code :lang<python>
125
132
s = 10
126
133
l = [1, 2, 3]
127
134
d = { a : 12, b : 99 }
@@ -130,6 +137,7 @@ Python
130
137
print l[2]
131
138
print d['a']
132
139
# 10, 2, 12
140
+ =end
133
141
134
142
Perl 6
135
143
@@ -155,6 +163,7 @@ In Perl 6, every block creates a lexical scope.
155
163
156
164
Python
157
165
166
+ = for code :lang<python>
158
167
if True:
159
168
x = 10
160
169
print x
@@ -177,6 +186,7 @@ Perl 6
177
186
178
187
Python
179
188
189
+ = for code :lang<python>
180
190
x = 10
181
191
for x in 1, 2, 3:
182
192
pass
@@ -196,6 +206,7 @@ Lambdas in Python can be written as blocks or pointy blocks in Perl 6.
196
206
197
207
Python
198
208
209
+ = for code :lang<python>
199
210
l = lambda i: i + 12
200
211
201
212
Perl 6
@@ -218,6 +229,7 @@ Another example (from the Python L<FAQ|https://docs.python.org/3/faq/programming
218
229
219
230
Python
220
231
232
+ = for code :lang<python>
221
233
squares = []
222
234
for x in range(5):
223
235
squares.append(lambda: x ** 2)
@@ -253,6 +265,7 @@ or symbol that can be used in Perl 6 has an ASCII equivalent
253
265
254
266
Python has C < for > loops and C < while > loops:
255
267
268
+ = for code :lang<python>
256
269
for i in 1, 2:
257
270
print i
258
271
j = 1
@@ -282,6 +295,7 @@ in Perl 6.
282
295
283
296
Python
284
297
298
+ = for code :lang<python>
285
299
for i in range(10):
286
300
if i == 3:
287
301
continue
@@ -306,12 +320,14 @@ in Perl 6. These both print 1, 2, 3.
306
320
307
321
Python
308
322
323
+ = begin code :lang<python>
309
324
def count():
310
325
for i in 1, 2, 3:
311
326
yield i
312
327
313
328
for c in count():
314
329
print c
330
+ = end code
315
331
316
332
Perl 6
317
333
@@ -333,24 +349,30 @@ Perl 6
333
349
Declaring a function (subroutine) with C < def > in Python is accomplished
334
350
with C < sub > in Perl 6.
335
351
352
+ = begin code :lang<python>
336
353
def add(a, b):
337
354
return a + b
338
355
339
356
sub add(\a, \b) {
340
357
return a + b
341
358
}
359
+ = end code
342
360
343
361
The C < return > is optional; the value of the last expression is used as
344
362
the return value:
345
363
364
+ = begin code
346
365
sub add(\a, \b) {
347
366
a + b
348
367
}
368
+ = end code
349
369
370
+ = begin code
350
371
# using variables with sigils
351
372
sub add($a, $b) {
352
373
$a + $b
353
374
}
375
+ = end code
354
376
355
377
Python 2 functions can be called with positional arguments
356
378
or keyword arguments. These are determined by the caller.
@@ -360,11 +382,13 @@ by the signature of the routine.
360
382
361
383
Python
362
384
385
+ = begin code :lang<python>
363
386
def speak(word, times):
364
387
for i in range(times):
365
388
print word
366
389
speak('hi', 2)
367
390
speak(word='hi', times=2)
391
+ = end code
368
392
369
393
Perl 6
370
394
@@ -397,6 +421,7 @@ could be made available by declaring a routine as a C<multi>.
397
421
398
422
Named parameters can be sent using a variety of formats:
399
423
424
+ sub hello {...};
400
425
# all the same
401
426
hello(name => 'world'); # fat arrow syntax
402
427
hello(:name('world')); # pair constructor
@@ -409,27 +434,29 @@ a pointy block.
409
434
410
435
Python
411
436
437
+ = for code :lang<python>
412
438
square = lambda x: x ** 2
413
439
414
440
Perl 6
415
441
416
- $square = sub ($x) { $x ** 2 }; # anonymous sub
417
- $square = -> $x { $x ** 2 }; # pointy block
418
- $square = { $^x ** 2 }; # placeholder variable
419
- $square = { $_ ** 2 }; # topic variable
442
+ my $square = sub ($x) { $x ** 2 }; # anonymous sub
443
+ my $square = -> $x { $x ** 2 }; # pointy block
444
+ my $square = { $^x ** 2 }; # placeholder variable
445
+ my $square = { $_ ** 2 }; # topic variable
420
446
421
447
Placeholder variables are lexicographically ordered to form positional parameters.
422
448
i.e. these are the same:
423
449
424
- $power = { $^x ** $^y }
425
- $power = -> $x, $y { $x ** $y }
450
+ my $power = { $^x ** $^y };
451
+ my $power = -> $x, $y { $x ** $y };
426
452
427
453
= head2 List Comprehensions
428
454
429
455
Postfix modifiers and anonymous functions can be combined to make list comprehensions.
430
456
431
457
Python
432
458
459
+ = for code :lang<python>
433
460
[ i * 2 for i in 3, 9 ]
434
461
435
462
Perl 6
@@ -441,6 +468,7 @@ Perl 6
441
468
Conditionals can be applied, but the C < if > comes first,
442
469
unlike in Python where the if comes second.
443
470
471
+ = for code :lang<python>
444
472
[ x * 2 for x in 1, 2, 3 if x > 1 ]
445
473
446
474
vs
450
478
For nested loops, the cross product operator C < X >
451
479
will help:
452
480
481
+ = for code :lang<python>
453
482
[ i + j for i in 3,9 for j in 2,10 ]
454
483
455
484
becomes either of these:
@@ -467,6 +496,7 @@ First, "instance variables", aka attributes in Perl 6:
467
496
468
497
Python:
469
498
499
+ = for code :lang<python>
470
500
class Dog:
471
501
def __init__(self, name):
472
502
self.name = name
@@ -482,13 +512,15 @@ and use the method C<new>.
482
512
483
513
Python
484
514
515
+ = for code :lang<python>
485
516
d = Dog('Fido')
486
517
e = Dog('Buddy')
487
518
print d.name
488
519
print e.name
489
520
490
521
Perl 6
491
522
523
+ class Dog {}; ...;
492
524
my $d = Dog.new(:name<Fido>);
493
525
my $e = Dog.new(:name<Buddy>);
494
526
say $d.name;
@@ -499,6 +531,7 @@ is to just declare a lexical variable and a method for accessing it.
499
531
500
532
Python:
501
533
534
+ = for code :lang<python>
502
535
class Dog:
503
536
kind = 'canine' # class attribute
504
537
def __init__(self, name):
@@ -529,6 +562,7 @@ To mutate attributes, in Perl 6 you"ll want to use C<is rw>:
529
562
530
563
Python:
531
564
565
+ = for code :lang<python>
532
566
class Dog:
533
567
def __init__(self, name):
534
568
self.name = name
@@ -547,6 +581,7 @@ Inheritance is done using C<is>:
547
581
548
582
Python
549
583
584
+ = begin code :lang<python>
550
585
class Animal:
551
586
def jump(self):
552
587
print ("I am jumping")
@@ -556,6 +591,7 @@ Python
556
591
557
592
d = Dog()
558
593
d.jump()
594
+ = end code
559
595
560
596
Perl 6
561
597
@@ -575,15 +611,20 @@ Multiple inheritance is possible by using C<is> multiple times, or with C<also>.
575
611
576
612
Python
577
613
614
+ = for code :lang<python>
578
615
class Dog(Animal, Friend, Pet):
579
616
pass
580
617
581
618
Perl 6
582
619
583
- class Dog is Animal is Friend is Pet { ... }
620
+ class Animal {}; class Friend {}; class Pet {};
621
+ ...;
622
+ class Dog is Animal is Friend is Pet {};
584
623
585
624
or
586
625
626
+ class Animal {}; class Friend {}; class Pet {};
627
+ ...;
587
628
class Dog is Animal {
588
629
also is Friend;
589
630
also is Pet;
@@ -599,6 +640,7 @@ in another one. In Perl 6, this is done with C<wrap>.
599
640
600
641
Python
601
642
643
+ = begin code :lang<python>
602
644
def greeter(f):
603
645
def new():
604
646
print 'hello'
@@ -610,6 +652,7 @@ Python
610
652
print 'world'
611
653
612
654
world();
655
+ = end code
613
656
614
657
Perl 6
615
658
@@ -650,6 +693,7 @@ or exiting a scope.
650
693
Here's a python context manager that prints the strings
651
694
'hello', 'world', and 'bye':
652
695
696
+ = begin code :lang<python>
653
697
class hello:
654
698
def __exit__(self, type, value, traceback):
655
699
print 'bye'
@@ -658,6 +702,7 @@ Here's a python context manager that prints the strings
658
702
659
703
with hello():
660
704
print 'world'
705
+ = end code
661
706
662
707
For enter and exit events, passing a block as
663
708
an argument would be one option:
@@ -687,5 +732,4 @@ X<|input (Python)>
687
732
688
733
See the L < prompt > function.
689
734
690
-
691
735
= end pod
0 commit comments