Perl 6 by example

66
Perl 6 by example

description

Going through existing Perl 6 programmes to see how early adopters use the language today, before Perl 6 is completed in any sence.

Transcript of Perl 6 by example

Page 1: Perl 6 by example

Perl 6by example

Page 2: Perl 6 by example

A talk about a kind of phylosophy

of Perl 6 learning process

Page 3: Perl 6 by example

proto.perl6.org

Page 4: Perl 6 by example

Proto is a hyper-lightweight dependency tracking and module installation system

Page 5: Perl 6 by example

pls is its new name

Page 6: Perl 6 by example

We don’t care of all that

Page 7: Perl 6 by example

What we do care of are

Page 8: Perl 6 by example

54 Perl 6 projects

are on proto.perl6.org

Page 9: Perl 6 by example

history.back()

Page 10: Perl 6 by example

2003

Page 11: Perl 6 by example

perl6.rulaunched

Page 12: Perl 6 by example
Page 13: Perl 6 by example

It observedthe code from

parrot/languages/perl6/examples

folder

Page 14: Perl 6 by example

Record #15

Example research. loop()

Page 15: Perl 6 by example

mandel.p6was the most exciting

Page 16: Perl 6 by example
Page 17: Perl 6 by example

loop ($y=30; $C = $y*0.1 - 1.5, $y--;) {   loop ($x=0; $c = $x*0.04 - 2.0, $z=0.0,    $Z=0.0, $x++ < 75;) {      loop ($r=$c, $i=$C, $k=0;       $t = $z*$z - $Z*$Z + $r,       $Z = 2.0*$z*$Z + $i, $z=$t, $k<112;       $k++) {   . . .

Page 18: Perl 6 by example

................::::::::::::::::::::::::::::::::::::::::::::...............

...........::::::::::::::::::::::::::::::::::::::::::::::::::::::..........

........::::::::::::::::::::::::::::::::::,,,,,,,:::::::::::::::::::.......

.....:::::::::::::::::::::::::::::,,,,,,,,,,,,,,,,,,,,,,:::::::::::::::....

...::::::::::::::::::::::::::,,,,,,,,,,,,;;;!:H!!;;;,,,,,,,,:::::::::::::..:::::::::::::::::::::::::,,,,,,,,,,,,,;;;;!!/>&*|& !;;;,,,,,,,:::::::::::::::::::::::::::::::::::,,,,,,,,,,,,,;;;;;!!//)|.*#|>/!;;;;;,,,,,,:::::::::::::::::::::::::::::,,,,,,,,,,,,;;;;;;!!!!//>|:    !:|//!!;;;;;,,,,,::::::::::::::::::::::::,,,,,,,,,,;;;;;;;!!/>>I>>)||I#     H&))>////*!;;,,,,::::::::::::::::::,,,,,,,,,,;;;;;;;;;!!!!/>H:  #|              IH&*I#/;;,,,,:::::::::::::,,,,,,,,,;;;;;!!!!!!!!!!//>|.H:                     #I>!!;;,,,,:::::::::,,,,,,,,,;;;;!/||>///>>///>>)|H                         %|&/;;,,,,,::::::,,,,,,,,;;;;;!!//)& :;I*,H#&||&/                           *)/!;;,,,,,::::,,,,,,;;;;;!!!//>)IH:,        ##                            #&!!;;,,,,,::::,;;;;!!!!!///>)H%.**           *                            )/!;;;,,,,,::::                                                          &)/!!;;;,,,,,::::,;;;;!!!!!///>)H%.**           *                            )/!;;;,,,,,::::,,,,,,;;;;;!!!//>)IH:,        ##                            #&!!;;,,,,,:::::,,,,,,,,;;;;;!!//)& :;I*,H#&||&/                           *)/!;;,,,,,:::::::,,,,,,,,,;;;;!/||>///>>///>>)|H                         %|&/;;,,,,,:::::::::::,,,,,,,,,;;;;;!!!!!!!!!!//>|.H:                     #I>!!;;,,,,::::::::::::::::,,,,,,,,,,;;;;;;;;;!!!!/>H:  #|              IH&*I#/;;,,,,::::::::::::::::::::::,,,,,,,,,,;;;;;;;!!/>>I>>)||I#     H&))>////*!;;,,,,::::::::::::::::::::::::::,,,,,,,,,,,,;;;;;;!!!!//>|:    !:|//!!;;;;;,,,,,:::::::::::::::::::::::::::::::,,,,,,,,,,,,,;;;;;!!//)|.*#|>/!;;;;;,,,,,,::::::::::::::::::::::::::::::::::::,,,,,,,,,,,,,;;;;!!/>&*|& !;;;,,,,,,,:::::::::::::...::::::::::::::::::::::::::,,,,,,,,,,,,;;;!:H!!;;;,,,,,,,,:::::::::::::.......:::::::::::::::::::::::::::::,,,,,,,,,,,,,,,,,,,,,,:::::::::::::::............::::::::::::::::::::::::::::::::::,,,,,,,:::::::::::::::::::..................::::::::::::::::::::::::::::::::::::::::::::::::::::::..........

Page 19: Perl 6 by example

history.now()

Page 20: Perl 6 by example
Page 21: Perl 6 by example

The method:try to understand

what you don’t understand

Page 22: Perl 6 by example

You’ll findimpressive things

Page 23: Perl 6 by example

mandelbrot project

Mandelbrot setin Perl 6

Page 24: Perl 6 by example

my $height = @*ARGS[0] // 31;my $width = $height;my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;my $lower-left = 1/2 - (5/4)i;

Page 25: Perl 6 by example

my $height = @*ARGS[0] // 31;my $width = $height;my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;my $lower-left = 1/2 - (5/4)i;

Global variable and defined-or

Page 26: Perl 6 by example

my $height = @*ARGS[0] // 31;my $width = $height;my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;my $lower-left = 1/2 - (5/4)i;

Complex numbers (wow!)

Page 27: Perl 6 by example

sub mandel(Complex $c) { my $z = 0i; for ^$max_iterations { $z = $z * $z + $c; return 1 if ($z.abs > 2); } return 0;}

Page 28: Perl 6 by example

sub mandel(Complex $c) { my $z = 0i; for ^$max_iterations { $z = $z * $z + $c; return 1 if ($z.abs > 2); } return 0;}0..$max_iterations range

Page 29: Perl 6 by example

for subdivide($upper-right.re, $lower-left.re, $height) -> $re { . . . (@line, $middle, @line.reverse).join(' ').say;}

Page 30: Perl 6 by example

for subdivide($upper-right.re, $lower-left.re, $height) -> $re { . . . (@line, $middle, @line.reverse).join(' ').say;}

Hyphens in variable names

Page 31: Perl 6 by example

for subdivide($upper-right.re, $lower-left.re, $height) -> $re { . . . (@line, $middle, @line.reverse).join(' ').say;}

for loop and its variable

Page 32: Perl 6 by example

for subdivide($upper-right.re, $lower-left.re, $height) -> $re { . . . (@line, $middle, @line.reverse).join(' ').say;}

Nested method calls

Page 33: Perl 6 by example

(@line, $middle, @line.reverse).map({ @color_map[$_] }).join(' ').say;

Method calls on a list

Page 34: Perl 6 by example

53 Perl 6 projects

still left

Page 35: Perl 6 by example

FakeDBI and

FakeDBD

Page 36: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Declaring and defining a class

Page 37: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Who is the author

Page 38: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Version number

Page 39: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Class variables

Page 40: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Not too easy to guess

Page 41: Perl 6 by example

bash-3.2$ grep '$!' * -r

S02-bits.pod: $!foo object attribute private storage

Page 42: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Twigils indicate private variables

Page 43: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Class method

Page 44: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Positional arguments

Page 45: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Named arguments

Page 46: Perl 6 by example

class FakeDBI:auth<mberends>:ver<0.0.1> { has $!err; has $!errstr; method connect( $dsn, $username, $password, :$RaiseError=0, :$PrintError=0, :$AutoCommit=1 ) {

Default values

Page 47: Perl 6 by example

given $drivername { when 'CSV' { . . . } when 'mysql' { . . . } default { . . . } }

given/when known from Perl 5.10 :-)

Page 48: Perl 6 by example

52 Perl 6 projects

still left

Page 49: Perl 6 by example

Really 52 left?

Page 50: Perl 6 by example

Much more!

Page 51: Perl 6 by example

perl6-exampleson github

module-managementparsersperlmonksshootouttutorialwsg

cookbookdoceulergamesinterpreterslib

Page 52: Perl 6 by example

perl6-exampleson github

module-managementparsersperlmonksshootouttutorialwsg

cookbookdoceulergamesinterpreterslib

Page 53: Perl 6 by example

eulerproject.netREADME prob005-unobe.pl prob025-polettix.plprob001-cspencer.pl prob006-polettix.pl prob029-polettix.plprob001-eric256.pl prob007-polettix.pl prob052-duff.plprob001-hexmode.pl prob008-duff.pl prob053-duff.plprob001-unobe.pl prob008-duff2.pl prob063-moritz.plprob002-eric256.pl prob009-polettix.pl prob063-polettix.plprob002-hexmode.pl prob010-polettix.pl prob081-matrix.txtprob003-eric256.pl prob011-moritz.pl prob081-moritz.plprob003-hexmode.pl prob012-polettix.pl prob092-moritz.plprob003-lanny.p6 prob017-duff.pl prob104-moritz.plprob004-unobe.pl prob024-moritz.pl

Page 54: Perl 6 by example

eulerproject.netREADME prob005-unobe.pl prob025-polettix.plprob001-cspencer.pl prob006-polettix.pl prob029-polettix.plprob001-eric256.pl prob007-polettix.pl prob052-duff.plprob001-hexmode.pl prob008-duff.pl prob053-duff.plprob001-unobe.pl prob008-duff2.pl prob063-moritz.plprob002-eric256.pl prob009-polettix.pl prob063-polettix.plprob002-hexmode.pl prob010-polettix.pl prob081-matrix.txtprob003-eric256.pl prob011-moritz.pl prob081-moritz.plprob003-hexmode.pl prob012-polettix.pl prob092-moritz.plprob003-lanny.p6 prob017-duff.pl prob104-moritz.plprob004-unobe.pl prob024-moritz.pl

Page 55: Perl 6 by example

# A simple implementation # of Eratosthenes' sievesub primes_iterator { return sub { state %D; state $q //= 2; $q //= 2;

Again, pure Perl 5.10 :-)

Page 56: Perl 6 by example

$ perl6 prob007-polettix.pl ===SORRY!===Symbol '%D' not predeclared in primes_iterator (prob007-polettix.pl:17)

Page 57: Perl 6 by example

my %D;my $q;

# A simple implementation # of Eratosthenes' sievesub primes_iterator { return sub { #state %D; #state $q //= 2; $q //= 2;

OK, let’s use global variables this time

Page 58: Perl 6 by example

$ perl6 prob007-polettix.pl

. . . Time passed. . .

result: 104743

Page 59: Perl 6 by example

my $it = primes_iterator();for 1 .. $nth - 1 -> $i { $it(); say "found $i primes so far" unless $i % 100;}say 'result: ', $it();

Subroutine reference in a scalar

Page 60: Perl 6 by example

51 + ∞ Perl 6 projects

still left

Page 61: Perl 6 by example

SVG.pm

Page 62: Perl 6 by example

my $svg = :svg[ :width(200), :height(200), circle => [ :cx(100), :cy(100), :r(50) ], text => [ :x(10), :y(20), "hello" ]];

Hash reference of hash references?

Page 63: Perl 6 by example

.perl() explains

Page 64: Perl 6 by example

say $svg.perl;

"svg" => ["width" => 200, "height" => 200, "circle" => ["cx" => 100, "cy" => 100, "r" => 50], "text" => ["x" => 10, "y" => 20, "hello"]]

Page 65: Perl 6 by example

50 + ∞ Perl 6 projects

still left