Skip to content

Commit fdcab66

Browse files
committed
test Perl 6 code in python page
Part of #1387
1 parent 9125507 commit fdcab66

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

doc/Language/py-nutshell.pod6

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ optional. A return is added to the end of the line.
1919
2020
Python 2
2121
22+
=for code :lang<python>
2223
print "Hello, world!"
2324
2425
Python 3
2526
27+
=for code :lang<python>
2628
print("Hello, world!")
2729
2830
Perl 6
@@ -34,6 +36,7 @@ the C<gist> method of its argument.
3436
3537
Perl 6
3638
39+
my $hello; ...;
3740
say "Hello, world!"; # also prints "Hello, world"
3841
say $hello; # same as: put $hello.gist
3942
@@ -66,6 +69,7 @@ is a closing curly brace followed by a newline.
6669
6770
Python
6871
72+
=for code :lang<python>
6973
print 1 + 2 + \
7074
3 + 4
7175
print ( 1 +
@@ -84,6 +88,7 @@ uses curly braces.
8488
8589
Python
8690
91+
=for code :lang<python>
8792
if 1 == 2:
8893
print "Wait, what?"
8994
else:
@@ -104,6 +109,7 @@ conditionals, as shown above.
104109
105110
In Python, variables are declared and initialized at the same time:
106111
112+
=for code :lang<python>
107113
foo = 12
108114
bar = 19
109115
@@ -122,6 +128,7 @@ Immutable variables can be sigil-less, if they are declared with a C<\>.
122128
123129
Python
124130
131+
=begin code :lang<python>
125132
s = 10
126133
l = [1, 2, 3]
127134
d = { a : 12, b : 99 }
@@ -130,6 +137,7 @@ Python
130137
print l[2]
131138
print d['a']
132139
# 10, 2, 12
140+
=end
133141
134142
Perl 6
135143
@@ -155,6 +163,7 @@ In Perl 6, every block creates a lexical scope.
155163
156164
Python
157165
166+
=for code :lang<python>
158167
if True:
159168
x = 10
160169
print x
@@ -177,6 +186,7 @@ Perl 6
177186
178187
Python
179188
189+
=for code :lang<python>
180190
x = 10
181191
for x in 1, 2, 3:
182192
pass
@@ -196,6 +206,7 @@ Lambdas in Python can be written as blocks or pointy blocks in Perl 6.
196206
197207
Python
198208
209+
=for code :lang<python>
199210
l = lambda i: i + 12
200211
201212
Perl 6
@@ -218,6 +229,7 @@ Another example (from the Python L<FAQ|https://docs.python.org/3/faq/programming
218229
219230
Python
220231
232+
=for code :lang<python>
221233
squares = []
222234
for x in range(5):
223235
squares.append(lambda: x ** 2)
@@ -253,6 +265,7 @@ or symbol that can be used in Perl 6 has an ASCII equivalent
253265
254266
Python has C<for> loops and C<while> loops:
255267
268+
=for code :lang<python>
256269
for i in 1, 2:
257270
print i
258271
j = 1
@@ -282,6 +295,7 @@ in Perl 6.
282295
283296
Python
284297
298+
=for code :lang<python>
285299
for i in range(10):
286300
if i == 3:
287301
continue
@@ -306,12 +320,14 @@ in Perl 6. These both print 1, 2, 3.
306320
307321
Python
308322
323+
=begin code :lang<python>
309324
def count():
310325
for i in 1, 2, 3:
311326
yield i
312327
313328
for c in count():
314329
print c
330+
=end code
315331
316332
Perl 6
317333
@@ -333,24 +349,30 @@ Perl 6
333349
Declaring a function (subroutine) with C<def> in Python is accomplished
334350
with C<sub> in Perl 6.
335351
352+
=begin code :lang<python>
336353
def add(a, b):
337354
return a + b
338355
339356
sub add(\a, \b) {
340357
return a + b
341358
}
359+
=end code
342360
343361
The C<return> is optional; the value of the last expression is used as
344362
the return value:
345363
364+
=begin code
346365
sub add(\a, \b) {
347366
a + b
348367
}
368+
=end code
349369
370+
=begin code
350371
# using variables with sigils
351372
sub add($a, $b) {
352373
$a + $b
353374
}
375+
=end code
354376
355377
Python 2 functions can be called with positional arguments
356378
or keyword arguments. These are determined by the caller.
@@ -360,11 +382,13 @@ by the signature of the routine.
360382
361383
Python
362384
385+
=begin code :lang<python>
363386
def speak(word, times):
364387
for i in range(times):
365388
print word
366389
speak('hi', 2)
367390
speak(word='hi', times=2)
391+
=end code
368392
369393
Perl 6
370394
@@ -397,6 +421,7 @@ could be made available by declaring a routine as a C<multi>.
397421
398422
Named parameters can be sent using a variety of formats:
399423
424+
sub hello {...};
400425
# all the same
401426
hello(name => 'world'); # fat arrow syntax
402427
hello(:name('world')); # pair constructor
@@ -409,27 +434,29 @@ a pointy block.
409434
410435
Python
411436
437+
=for code :lang<python>
412438
square = lambda x: x ** 2
413439
414440
Perl 6
415441
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
420446
421447
Placeholder variables are lexicographically ordered to form positional parameters.
422448
i.e. these are the same:
423449
424-
$power = { $^x ** $^y }
425-
$power = -> $x, $y { $x ** $y }
450+
my $power = { $^x ** $^y };
451+
my $power = -> $x, $y { $x ** $y };
426452
427453
=head2 List Comprehensions
428454
429455
Postfix modifiers and anonymous functions can be combined to make list comprehensions.
430456
431457
Python
432458
459+
=for code :lang<python>
433460
[ i * 2 for i in 3, 9 ]
434461
435462
Perl 6
@@ -441,6 +468,7 @@ Perl 6
441468
Conditionals can be applied, but the C<if> comes first,
442469
unlike in Python where the if comes second.
443470
471+
=for code :lang<python>
444472
[ x * 2 for x in 1, 2, 3 if x > 1 ]
445473
446474
vs
@@ -450,6 +478,7 @@ vs
450478
For nested loops, the cross product operator C<X>
451479
will help:
452480
481+
=for code :lang<python>
453482
[ i + j for i in 3,9 for j in 2,10 ]
454483
455484
becomes either of these:
@@ -467,6 +496,7 @@ First, "instance variables", aka attributes in Perl 6:
467496
468497
Python:
469498
499+
=for code :lang<python>
470500
class Dog:
471501
def __init__(self, name):
472502
self.name = name
@@ -482,13 +512,15 @@ and use the method C<new>.
482512
483513
Python
484514
515+
=for code :lang<python>
485516
d = Dog('Fido')
486517
e = Dog('Buddy')
487518
print d.name
488519
print e.name
489520
490521
Perl 6
491522
523+
class Dog {}; ...;
492524
my $d = Dog.new(:name<Fido>);
493525
my $e = Dog.new(:name<Buddy>);
494526
say $d.name;
@@ -499,6 +531,7 @@ is to just declare a lexical variable and a method for accessing it.
499531
500532
Python:
501533
534+
=for code :lang<python>
502535
class Dog:
503536
kind = 'canine' # class attribute
504537
def __init__(self, name):
@@ -529,6 +562,7 @@ To mutate attributes, in Perl 6 you"ll want to use C<is rw>:
529562
530563
Python:
531564
565+
=for code :lang<python>
532566
class Dog:
533567
def __init__(self, name):
534568
self.name = name
@@ -547,6 +581,7 @@ Inheritance is done using C<is>:
547581
548582
Python
549583
584+
=begin code :lang<python>
550585
class Animal:
551586
def jump(self):
552587
print ("I am jumping")
@@ -556,6 +591,7 @@ Python
556591
557592
d = Dog()
558593
d.jump()
594+
=end code
559595
560596
Perl 6
561597
@@ -575,15 +611,20 @@ Multiple inheritance is possible by using C<is> multiple times, or with C<also>.
575611
576612
Python
577613
614+
=for code :lang<python>
578615
class Dog(Animal, Friend, Pet):
579616
pass
580617
581618
Perl 6
582619
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 {};
584623
585624
or
586625
626+
class Animal {}; class Friend {}; class Pet {};
627+
...;
587628
class Dog is Animal {
588629
also is Friend;
589630
also is Pet;
@@ -599,6 +640,7 @@ in another one. In Perl 6, this is done with C<wrap>.
599640
600641
Python
601642
643+
=begin code :lang<python>
602644
def greeter(f):
603645
def new():
604646
print 'hello'
@@ -610,6 +652,7 @@ Python
610652
print 'world'
611653
612654
world();
655+
=end code
613656
614657
Perl 6
615658
@@ -650,6 +693,7 @@ or exiting a scope.
650693
Here's a python context manager that prints the strings
651694
'hello', 'world', and 'bye':
652695
696+
=begin code :lang<python>
653697
class hello:
654698
def __exit__(self, type, value, traceback):
655699
print 'bye'
@@ -658,6 +702,7 @@ Here's a python context manager that prints the strings
658702
659703
with hello():
660704
print 'world'
705+
=end code
661706
662707
For enter and exit events, passing a block as
663708
an argument would be one option:
@@ -687,5 +732,4 @@ X<|input (Python)>
687732
688733
See the L<prompt> function.
689734
690-
691735
=end pod

xt/examples-compilation.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ if @*ARGS {
3131
doc/Language/modules.pod6
3232
doc/Language/phasers.pod6
3333
doc/Language/pod.pod6
34-
doc/Language/py-nutshell.pod6
3534
doc/Language/tables.pod6
3635
>);
3736
push @files, $file;

0 commit comments

Comments
 (0)