Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

6 more things about Perl 6

739 views

Published on

A selection of things I like about Perl 6.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

6 more things about Perl 6

  1. 1. 6 more things about 6 Boston.pm, 10 Jan 2017
  2. 2. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6
  3. 3. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 perlmodules.net
  4. 4. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Rats
  5. 5. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 $ perl -le 'print 0.3 - 0.2- 0.1' -2.77555756156289e-17 $ perl6 -e 'put 0.3 - 0.2 - 0.1' 0
  6. 6. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 $ perl6 To exit type 'exit' or '^D' > 0.1 0.1 > 0.1.^name Rat > 0.1.numerator 1 > 0.1.denominator 10
  7. 7. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 $ perl6 To exit type 'exit' or '^D' > 1/3 + 1/4 0.583333 > (1/3 + 1/4).denominator 12
  8. 8. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Soft Failures
  9. 9. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 CATCH { default { put "Caught something" } } my $fh = open 'not-there'; # this fails put "Result is " ~ $fh.^name; unless $fh { # $fh.so given $fh.exception { put "failure: {.^name}: {.message}"; } } Result is Failure failure: X::AdHoc: Failed to open file
  10. 10. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Resume
  11. 11. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 { CATCH { default { put "Caught {.^name}: {.message}" } } my $fh = open 'not-there', :r; my $line = $fh.line; put "I'm still going"; } Caught X::AdHoc: Failed to open file
  12. 12. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 { CATCH { default { put "Caught {.^name}: {.message}"; .resume } } my $fh = open 'not-there', :r; my $line = $fh.line; put "I'm still going"; } Caught X::AdHoc: Failed to open file I’m still going
  13. 13. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Interpolation
  14. 14. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my $scalar = 'Hamadryas'; my @array = qw/ Dog Cat Bird /; my %hash = a => 1, b => 2, c => 3; put "scalar: $scalar"; put "array: @array[]"; put "hash: %hash{}"; scalar: Hamadryas array: Dog Cat Bird hash: a 1 b 2 c 3
  15. 15. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 $_ = 'Hamadryas'; put "scalar: { $_ }"; put "scalar: { 1 + 2 }"; put "scalar: { .^name }"; scalar: Hamadryas scalar: 3 scalar: Str
  16. 16. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 fmt
  17. 17. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my $s = <22/7>; put $s.fmt( '%5.4f' ); put $s.fmt( '%.14f' ); 3.1429 3.14285714285714
  18. 18. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @s = 1 .. 8; @s .map( (*/7).fmt('%.5f') ) .join( "n" ).put; 0.14286 0.28571 0.42857 0.57143 0.71429 0.85714 1.00000 1.14286
  19. 19. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Lists of Lists
  20. 20. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my $scalar = ( 1, 2, 3 ); # my $scalar = 1, 2, 3; # Nope! put "scalar: $scalar"; put "scalar: { $scalar.^name }"; scalar: 1 2 3 scalar: List
  21. 21. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @array = ( ( 1, 2 ), qw/a b/ ); put "elems: { @array.elems }"; put "@array[]"; put "@array[0]"; put "{ @array[0].^name }"; 2 1 2 a b 1 2 List
  22. 22. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my $x = ( 1, 2, 6 ); some_sub( $x ); sub some_sub ( $x ) { put "x has { $x.elems }"; } x has 3
  23. 23. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my Buf $buf = Buf.new( 0xDE, 0xAD, 0xBE, 0xEF ); for $buf.values -> $c { put "c is $c"; } c is 222 c is 173 c is 190 c is 239
  24. 24. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my Buf $buf = Buf.new( 0xDE, 0xAD, 0xBE, 0xEF ); for $buf.rotor(2) -> $c { put "c is $c"; } c is 222 173 c is 190 239
  25. 25. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my Buf $buf = Buf.new( 0xDE, 0xAD, 0xBE, 0xEF ); for $buf.rotor(2) -> $c { put "c is $c"; put "word is ", ( $c[0] +< 8 + $c[1] ) .fmt( '%02X' ); } c is 222 173 word is DEAD c is 190 239 word is BEEF
  26. 26. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @a = 'a', ('b', 'c' ); my @b = 'd', 'e', 'f', @a; my @c = 'x', $( 'y', 'z' ), 'w'; my @ab = @a, @b, @c; say "ab: ", @ab; ab: [[a (b c)] [d e f [a (b c)]] [x (y z) w]]
  27. 27. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @f = @ab; while @f.grep: *.elems > 1 { @f = @f.map: *.Slip; }; ( a b c d e f a b c x y z w )
  28. 28. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @f = @ab; @f = slippery( @f ); sub slippery ( $l, $level = 0 ) { put "t" x $level, "Got ", $l; my @F; for $l.list { when .elems == 1 { push @F, $_ } default { push @F, slippery($_,$level + 1 ) } } @F.Slip; } ( a b c d e f a b c x y z w )
  29. 29. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @f = @ab; my @f2; while @f { @f[0].elems == 1 ?? @f2.push( @f.shift ) !! @f.unshift( @f.shift.Slip ) } ( a b c d e f a b c x y z w )
  30. 30. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @f = @ab; @f = gather slippery( @f ); sub slippery ( $l ) { for $l.list { say "Processing $_"; .elems == 1 ?? take $_ !! slippery( $_ ); } ( a b c d e f a b c x y z w )
  31. 31. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 my @f = @ab; @f = gather { while @f { @f[0].elems == 1 ?? take @f.shift !! @f.unshift( @f.shift.Slip ) } } ( a b c d e f a b c x y z w )
  32. 32. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 sub prefix:<__>( $listy ) { gather { while @f { @f[0].elems == 1 ?? take @f.shift !! @f.unshift( @f.shift.Slip ) } } } my @f = @ab; @f = __@f; ( a b c d e f a b c x y z w )
  33. 33. ThePerlReview•www.theperlreview.com 6MoreThingsAbout6 Questions

×