Perl 6
by example
A talk about
a kind of phylosophy
       of Perl 6
  learning process
proto.perl6.org
Proto is a hyper-
lightweight
dependency tracking
and module
installation system
pls is its
new name
We don’t care
 of all that
What we
do care of are
54
  Perl 6 projects
are on proto.perl6.org
history.back()
2003
perl6.ru
launched
It observed
 the code from
parrot/languages/
 perl6/examples
      folder
Record #15

Example research.
     loop()
mandel.p6
was the most exciting
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 ($...
................::::::::::::::::::::::::::::::::::::::::::::...............
...........:::::::::::::::::::::::::::::::::::...
history.now()
The method:
try to understand
     what you
don’t understand
You’ll find
impressive things
mandelbrot project

 Mandelbrot set
    in Perl 6
my $height = @*ARGS[0] // 31;
my $width = $height;
my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;
my $lower-left...
my $height = @*ARGS[0] // 31;
my $width = $height;
my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;
my $lower-left...
my $height = @*ARGS[0] // 31;
my $width = $height;
my $max_iterations = 50;

my $upper-right = -2 + (5/4)i;
my $lower-left...
sub mandel(Complex $c) {
  my $z = 0i;
  for ^$max_iterations {
     $z = $z * $z + $c;
     return 1 if ($z.abs > 2);
  }...
sub mandel(Complex $c) {
  my $z = 0i;
  for ^$max_iterations {
     $z = $z * $z + $c;
     return 1 if ($z.abs > 2);
  }...
for subdivide($upper-right.re,
   $lower-left.re, $height) -> $re {
   ...
   (@line, $middle,
    @line.reverse).join(' '...
for subdivide($upper-right.re,
   $lower-left.re, $height) -> $re {
   ...
   (@line, $middle,
    @line.reverse).join(' '...
for subdivide($upper-right.re,
   $lower-left.re, $height) -> $re {
   ...
   (@line, $middle,
    @line.reverse).join(' '...
for subdivide($upper-right.re,
   $lower-left.re, $height) -> $re {
   ...
   (@line, $middle,
    @line.reverse).join(' '...
(@line, $middle, @line.reverse).map
({ @color_map[$_] }).join(' ').say;




Method calls on a list
53
Perl 6 projects
    still left
FakeDBI
  and
FakeDBD
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
bash-3.2$ grep '$!' * -r

S02-bits.pod:
  $!foo     object attribute
  private storage
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
class FakeDBI:auth<mberends>:ver<0.0.1> {
   has $!err;
   has $!errstr;
   method connect(
     $dsn, $username, $passwor...
given $drivername {
    when 'CSV'   {...}
    when 'mysql' { . . . }
    default      {...}
}




 given/when known from ...
52
Perl 6 projects
    still left
Really 52 left?
Much more!
perl6-examples
    on github
cookbook       module-management
doc            parsers
euler          perlmonks
games       ...
perl6-examples
    on github
cookbook       module-management
doc            parsers
euler          perlmonks
games       ...
eulerproject.net
README
    
 
       prob005-unobe.pl
 prob025-polettix.pl
prob001-cspencer.pl
 prob006-polettix.pl
 prob...
eulerproject.net
README
    
 
       prob005-unobe.pl
 prob025-polettix.pl
prob001-cspencer.pl
 prob006-polettix.pl
 prob...
# A simple implementation
# of Eratosthenes' sieve
sub primes_iterator {
  return sub {
    state %D;
    state $q //= 2;
...
$ perl6 prob007-polettix.pl
===SORRY!===
Symbol '%D' not predeclared in primes_iterator
(prob007-polettix.pl:17)
my %D;
my $q;

# A simple implementation
# of Eratosthenes' sieve
sub primes_iterator {
  return sub {
    #state %D;
    ...
$ perl6 prob007-polettix.pl

   . . . Time passed. . .

result: 104743
my $it = primes_iterator();
for 1 .. $nth - 1 -> $i {
  $it();
  say "found $i primes so far" unless $i % 100;
}
say 'resu...
51 + ∞
Perl 6 projects
    still left
SVG.pm
my $svg = :svg[
    :width(200), :height(200),
    circle => [
       :cx(100), :cy(100), :r(50)
    ],
    text => [
    ...
.perl() explains
say $svg.perl;

"svg" => ["width" => 200,
"height" => 200, "circle" =>
["cx" => 100, "cy" => 100, "r"
=> 50], "text" => ["...
50 + ∞
Perl 6 projects
    still left
Andrew Shitov
talks.shitov.ru | andy@shitov.ru
Perl 6 by example
Perl 6 by example
Perl 6 by example
Upcoming SlideShare
Loading in …5
×

Perl 6 by example

1,952
-1

Published on

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

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,952
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
31
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Perl 6 by example

  1. 1. Perl 6 by example
  2. 2. A talk about a kind of phylosophy of Perl 6 learning process
  3. 3. proto.perl6.org
  4. 4. Proto is a hyper- lightweight dependency tracking and module installation system
  5. 5. pls is its new name
  6. 6. We don’t care of all that
  7. 7. What we do care of are
  8. 8. 54 Perl 6 projects are on proto.perl6.org
  9. 9. history.back()
  10. 10. 2003
  11. 11. perl6.ru launched
  12. 12. It observed the code from parrot/languages/ perl6/examples folder
  13. 13. Record #15 Example research. loop()
  14. 14. mandel.p6 was the most exciting
  15. 15. 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++) {    . . .
  16. 16. ................::::::::::::::::::::::::::::::::::::::::::::............... ...........::::::::::::::::::::::::::::::::::::::::::::::::::::::.......... ........::::::::::::::::::::::::::::::::::,,,,,,,:::::::::::::::::::....... .....:::::::::::::::::::::::::::::,,,,,,,,,,,,,,,,,,,,,,:::::::::::::::.... ...::::::::::::::::::::::::::,,,,,,,,,,,,;;;!: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!!;;;,,,,,,,,:::::::::::::.. .....:::::::::::::::::::::::::::::,,,,,,,,,,,,,,,,,,,,,,:::::::::::::::.... ........::::::::::::::::::::::::::::::::::,,,,,,,:::::::::::::::::::....... ...........::::::::::::::::::::::::::::::::::::::::::::::::::::::..........
  17. 17. history.now()
  18. 18. The method: try to understand what you don’t understand
  19. 19. You’ll find impressive things
  20. 20. mandelbrot project Mandelbrot set in Perl 6
  21. 21. 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;
  22. 22. 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
  23. 23. 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!)
  24. 24. sub mandel(Complex $c) { my $z = 0i; for ^$max_iterations { $z = $z * $z + $c; return 1 if ($z.abs > 2); } return 0; }
  25. 25. 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
  26. 26. for subdivide($upper-right.re, $lower-left.re, $height) -> $re { ... (@line, $middle, @line.reverse).join(' ').say; }
  27. 27. for subdivide($upper-right.re, $lower-left.re, $height) -> $re { ... (@line, $middle, @line.reverse).join(' ').say; } Hyphens in variable names
  28. 28. for subdivide($upper-right.re, $lower-left.re, $height) -> $re { ... (@line, $middle, @line.reverse).join(' ').say; } for loop and its variable
  29. 29. for subdivide($upper-right.re, $lower-left.re, $height) -> $re { ... (@line, $middle, @line.reverse).join(' ').say; } Nested method calls
  30. 30. (@line, $middle, @line.reverse).map ({ @color_map[$_] }).join(' ').say; Method calls on a list
  31. 31. 53 Perl 6 projects still left
  32. 32. FakeDBI and FakeDBD
  33. 33. 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
  34. 34. 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
  35. 35. 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
  36. 36. 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
  37. 37. 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
  38. 38. bash-3.2$ grep '$!' * -r S02-bits.pod: $!foo object attribute private storage
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. 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
  43. 43. 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
  44. 44. given $drivername { when 'CSV' {...} when 'mysql' { . . . } default {...} } given/when known from Perl 5.10 :-)
  45. 45. 52 Perl 6 projects still left
  46. 46. Really 52 left?
  47. 47. Much more!
  48. 48. perl6-examples on github cookbook module-management doc parsers euler perlmonks games shootout interpreters tutorial lib wsg
  49. 49. perl6-examples on github cookbook module-management doc parsers euler perlmonks games shootout interpreters tutorial lib wsg
  50. 50. eulerproject.net README prob005-unobe.pl prob025-polettix.pl prob001-cspencer.pl prob006-polettix.pl prob029-polettix.pl prob001-eric256.pl prob007-polettix.pl prob052-duff.pl prob001-hexmode.pl prob008-duff.pl prob053-duff.pl prob001-unobe.pl prob008-duff2.pl prob063-moritz.pl prob002-eric256.pl prob009-polettix.pl prob063-polettix.pl prob002-hexmode.pl prob010-polettix.pl prob081-matrix.txt prob003-eric256.pl prob011-moritz.pl prob081-moritz.pl prob003-hexmode.pl prob012-polettix.pl prob092-moritz.pl prob003-lanny.p6 prob017-duff.pl prob104-moritz.pl prob004-unobe.pl prob024-moritz.pl
  51. 51. eulerproject.net README prob005-unobe.pl prob025-polettix.pl prob001-cspencer.pl prob006-polettix.pl prob029-polettix.pl prob001-eric256.pl prob007-polettix.pl prob052-duff.pl prob001-hexmode.pl prob008-duff.pl prob053-duff.pl prob001-unobe.pl prob008-duff2.pl prob063-moritz.pl prob002-eric256.pl prob009-polettix.pl prob063-polettix.pl prob002-hexmode.pl prob010-polettix.pl prob081-matrix.txt prob003-eric256.pl prob011-moritz.pl prob081-moritz.pl prob003-hexmode.pl prob012-polettix.pl prob092-moritz.pl prob003-lanny.p6 prob017-duff.pl prob104-moritz.pl prob004-unobe.pl prob024-moritz.pl
  52. 52. # A simple implementation # of Eratosthenes' sieve sub primes_iterator { return sub { state %D; state $q //= 2; $q //= 2; Again, pure Perl 5.10 :-)
  53. 53. $ perl6 prob007-polettix.pl ===SORRY!=== Symbol '%D' not predeclared in primes_iterator (prob007-polettix.pl:17)
  54. 54. my %D; my $q; # A simple implementation # of Eratosthenes' sieve sub primes_iterator { return sub { #state %D; #state $q //= 2; $q //= 2; OK, let’s use global variables this time
  55. 55. $ perl6 prob007-polettix.pl . . . Time passed. . . result: 104743
  56. 56. 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
  57. 57. 51 + ∞ Perl 6 projects still left
  58. 58. SVG.pm
  59. 59. 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?
  60. 60. .perl() explains
  61. 61. say $svg.perl; "svg" => ["width" => 200, "height" => 200, "circle" => ["cx" => 100, "cy" => 100, "r" => 50], "text" => ["x" => 10, "y" => 20, "hello"]]
  62. 62. 50 + ∞ Perl 6 projects still left
  63. 63. Andrew Shitov talks.shitov.ru | andy@shitov.ru
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×