Python to Perl 6 - nutshell
Learning Perl 6 from Python, in a nutshell
This page is an attempt to provide a way to learn Perl 6 for folks coming from a Python background. We discuss the equivalent syntax in Perl 6 for a number of Python constructs and idioms.
Basic syntax
Hello, world
Let's start with printing "Hello, world!". The put keyword in Perl 6 is the equivalent of print in Python. Like Python 2, parentheses are optional. A newline is added to the end of the line.
Python 2
print "Hello, world!"
Python 3
print("Hello, world!")
Perl 6
put "Hello, world!"
There is also the say keyword, which behaves similarly, but will call the gist method of its argument.
Perl 6
my = "Hello, world!";say ; # also prints "Hello, world!"# same as: put $hello.gist
In Python, '
and "
are interchangeable. In Perl 6, both may be used for quoting, but double quotes ("
) signify that interpolation should be performed. For instance, variables that start with a $
, and expressions contained in curly braces are interpolated.
Perl 6
my = 'earth';say "Hello, $planet"; # Hello, earthsay 'Hello, $planet'; # Hello, $planetsay "Hello, planet number "; # Hello, planet number 3
Statement separators
In Python, a newline signifies the end of a statement. There are a few exceptions: A backslash before a newline continues a statement across lines. Also if there is an unmatched opening parentheses, square bracket, or curly brace, the statement continues across lines, until the matching curly braces are closed.
In Perl 6, a semicolon signifies the end of a statement. The semicolon may be omitted if it is the last statement of a block. The semicolon may also be omitted if there is a closing curly brace followed by a newline.
Python
print 1 + 2 + \3 + 4print ( 1 +2 )
Perl 6
say 1 + 2 +3 + 4;say 1 +2;
Blocks
In Python, indentation is used to indicate a block. Perl 6 uses curly braces.
Python
if 1 == 2:print "Wait, what?"else:print "1 is not 2."
Perl 6
if 1 == 2else
Parentheses are optional in both languages for expressions in conditionals, as shown above.
Variables
In Python, variables are declared and initialized at the same time:
foo = 12bar = 19
In Perl 6, the my
declarator declares a lexical variable. A variable can be initialized with =
. This variable can either be declared first and later initialized or declared and initialized at once.
my ; # declare= 12; # initializemy = 19; # both at once
Also, as you may have noticed, variables in Perl 6 usually start with sigils -- symbols indicating the type of their container. Variables starting with a $
hold scalars. Variables starting with an @
hold arrays, and variables starting with a %
hold a hash (dict). Sigilless variables, declared with a \
but used without them, are bound to the value they are assigned to and are thus immutable.
Please note that, from now on, we are going to use sigilless variables in most examples just to illustrate the similarity with Python. That is technically correct, but in general we are going to use sigilless variables in places where their immutability (or independence of type, when they are used in signatures) is needed or needs to be highlighted.
Python
s = 10l = [1, 2, 3]d =print sprint l[2]print d['a']# 10, 2, 12
Perl 6
my = 10;my = 1, 2, 3;my = a => 12, b => 99;my \x = 99;say ;say [1];say <a>; # or %d{'a'}say x;# 10, 2, 12, 99
Scope
In Python, functions and classes create a new scope, but no other block constructor (e.g. loops, conditionals) creates a scope. In Python 2, list comprehensions do not create a new scope, but in Python 3, they do.
In Perl 6, every block creates a lexical scope.
Python
if True:x = 10print x# x is now 10
Perl 6
if Truesay# error, $x is not declared in this scope
my ;if Truesay# ok, $x is 10
Python
x = 10for x in 1, 2, 3:passprint x# x is 3
Perl 6
my \x = 10;for 1, 2, 3 -> \xsay x;# x is 10
Lambdas in Python can be written as blocks or pointy blocks in Perl 6.
Python
l = lambda i: i + 12
Perl 6
my = ->
Another Perl 6 idiom for constructing lambdas is the Whatever star, *
.
Perl 6
my = * + 12 # same as above
A *
in an expression will become a placeholder for the argument, and transform the expression into a lambda at compile time. Each *
in an expression is a separate positional parameter.
See the section below for more constructs regarding subroutines and blocks.
Another example (from the Python FAQ):
Python
squares = []for x in range(5):squares.append(lambda: x ** 2)print squares[2]()print squares[4]()# both 16 since there is only one x
Perl 6
my \squares = [];for ^5 -> \xsay squares[2]();say squares[4]();# 4, 16 since each loop iteration has a lexically scoped x,
Note that ^N
is like range(N)
. Similarly, N..^M
works like range(N, M)
(a list from N to M - 1). The range N..M
is a list from N to M. The ^
before or after the ..
indicates that the beginning or ending endpoint of the list (or both) should be excluded.
Also, x²
is a cute way of writing x ** 2
(which also works fine); the unicode superscript 2 squares a number. Many of the other unicode operators work as you would expect (exponents, fractions, π), but every unicode operator or symbol that can be used in Perl 6 has an ASCII equivalent.
Control flow
Python has for
loops and while
loops:
for i in 1, 2:print ij = 1while j < 3:print jj += 1
# 1, 2, 1, 2
Perl 6 also has for
loops and while
loops:
for 1, 2 ->my = 1;while < 3
(Perl 6 also has a few more looping constructs: repeat...until
, repeat...while
, until
, and loop
.)
last
leaves a loop in Perl 6, and is analogous to break
in Python. continue
in Python is next
in Perl 6.
Python
for i in range(10):if i == 3:continueif i == 5:breakprint i
Perl 6
for ^10 ->
Using if
as a statement modifier (as above) is acceptable in Perl 6, even outside of a list comprehension.
The yield
statement within a for
loop in Python, which produces a generator
, is like a gather
/take
construct in Perl 6. These both print 1, 2, 3.
Python
def count():for i in 1, 2, 3:yield ifor c in count():print c
Perl 6
sub countfor count() ->
Lambdas, functions and subroutines
Declaring a function (subroutine) with def
in Python is accomplished with sub
in Perl 6.
def add(a, b):return a + bsub add(\a, \b)
The return
is optional; the value of the last expression is used as the return value:
sub add(\a, \b)
# using variables with sigilssub add(, )
Python 2 functions can be called with positional arguments or keyword arguments. These are determined by the caller. In Python 3, some arguments may be "keyword only". In Perl 6, positional and named arguments are determined by the signature of the routine.
Python
def speak(word, times):for i in range(times):print wordspeak('hi', 2)speak(word='hi', times=2)
Perl 6
Positional parameters:
sub speak(, )speak('hi', 2);
Named parameters start with a colon:
sub speak(:, :)speak(word => 'hi', times => 2);speak(:word<hi>, :times<2>); # Alternative, more idiomatic
Perl 6 supports multiple dispatch, so several signatures could be made available by declaring a routine as a multi
.
multi sub speak(, )multi sub speak(:, :)speak('hi', 2);speak(:word<hi>, :times<2>);
Named parameters can be sent using a variety of formats:
sub hello ;# all the samehello(name => 'world'); # fat arrow syntaxhello(:name('world')); # pair constructorhello :name<world>; # <> quotes words and makes a listmy = 'world';hello(:); # lexical var with the same name
Creating an anonymous function can be done with sub
, with a block or with a pointy block.
Python
square = lambda x: x ** 2
Perl 6
my = sub () ; # anonymous submy = -> ; # pointy blockmy = ; # placeholder variablemy = ; # topic variable
Placeholder variables are lexicographically ordered to form positional parameters. Thus these are the same:
my = ;my = -> , ;
List comprehensions
Postfix statement modifiers and blocks can be combined to easily create list comprehensions in Perl 6.
Python
print [ i * 2 for i in 3, 9 ] # OUTPUT: «[6, 18]»
Perl 6
say ( * 2 for 3, 9 ); # OUTPUT: «(6 18)»say ( for 3, 9 ); # OUTPUT: «(6 18)»say ( -> \i for 3, 9 ); # OUTPUT: «(6 18)»
Conditionals can be applied, but the if
keyword comes first, unlike in Python where the if comes second.
print [ x * 2 for x in 1, 2, 3 if x > 1 ] # OUTPUT: «[4, 6]»
vs
say ( * 2 if > 1 for 1, 2, 3 ); # OUTPUT: «(4 6)»
For nested loops, the cross product operator X
will help:
print [ i + j for i in 3,9 for j in 2,10 ] # OUTPUT: «[5, 13, 11, 19]»
becomes either of these:
say ( for (3,9) X (2,10) ); # OUTPUT: «(5 13 11 19)»say ( -> (\i, \j) for (3,9) X (2,10) ); # OUTPUT: «(5 13 11 19)»say ( -> (, ) for (3,9) X (2,10) );# OUTPUT: «(5 13 11 19)»say ( for (3,9) X (2,10) ); # OUTPUT: «(5 13 11 19)»
Using map
(which is just like Python's map
) and grep
(which is like Python's filter
) is an alternative.
Classes and objects
Here's an example from the Python docs. First let's go over "instance variables" which are known as attributes in Perl 6:
Python:
:def __init__(self, name):self.name = name
Perl 6:
For each created class, Perl 6 provides the constructor method new
by default which takes named arguments.
Python:
d = Dog('Fido')e = Dog('Buddy')print d.nameprint e.name
Perl 6
my = Dog.new(:name<Fido>); # or: Dog.new(name => 'Fido')my = Dog.new(:name<Buddy>);say .name;say .name;
Class attributes in Perl 6 can be declared in a few ways. One way is to just declare a lexical variable and a method for accessing it.
Python:
:kind = 'canine' # class attributedef __init__(self, name):self.name = name # instance attributed = Dog('Fido')e = Dog('Buddy')print d.kindprint e.kindprint d.nameprint e.name
Perl 6:
my = Dog.new(:name<Fido>);my = Dog.new(:name<Buddy>);say .kind;say .kind;say .name;say .name;
In order to mutate attributes in Perl 6, you must use the is rw
trait on the attributes:
Python:
:def __init__(self, name):self.name = named = Dog()d.name = 'rover'
Perl 6:
my = Dog.new;.name = 'rover';
Inheritance is done using is
:
Python
:def jump(self):print ("I am jumping")(Animal):passd = Dog()d.jump()
Perl 6
is Animalmy = Dog.new;.jump;
Multiple inheritance is possible by using the is
trait as many times as required. Alternatively, it can be used in conjunction with the also
keyword.
Python
(Animal, Friend, Pet):pass
Perl 6
; ; ;...;is Animal is Friend is Pet ;
or
; ; ;...;is Animal
Decorators
Decorators in Python are a way of wrapping a function in another one. In Perl 6, this is done with wrap
.
Python
def greeter(f):def new():print 'hello'f()return newdef world():print 'world'world();
Perl 6
sub world.wrap(sub ());world;
An alternative would be to use a trait:
# declare the trait 'greeter'multi sub trait_mod:<is>(Routine , :)sub world is greeterworld;
Context managers
Context managers in Python declare actions that happen when entering or exiting a scope.
Here's a Python context manager that prints the strings 'hello', 'world', and 'bye'.
:def __exit__(self, type, value, traceback):print 'bye'def __enter__(self):print 'hello'with hello():print 'world'
For "enter" and "exit" events, passing a block as an argument would be one option:
sub hello(Block )hello
A related idea is 'Phasers' which may be set up to run on entering or leaving a block.
input
In Python 3, the input
keyword is used to prompt the user. This keyword can be provided with an optional argument which is written to standard output without a trailing newline:
user_input = input("Say hi → ")print(user_input)
When prompted, you can enter Hi
or any other string, which will be stored in the user_input
variable. This is similar to prompt in Perl 6:
my = prompt("Say hi → ");say ; # OUTPUT: whatever you entered.