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 things about perl 6

829 views

Published on

Six things I like in Perl 6.

Published in: Technology

6 things about perl 6

  1. 1. 6 things about 6 NY.pm, 8 Dec 2016
  2. 2. ThePerlReview•www.theperlreview.com 6ThingsAbout6 perlmodules.net
  3. 3. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Phasers
  4. 4. ThePerlReview•www.theperlreview.com 6ThingsAbout6 count = 0; while ( … ) { … } output "There were {count} items!";
  5. 5. ThePerlReview•www.theperlreview.com 6ThingsAbout6 use v6; while( … ) { state $count = 0; LAST { put "There were $count" } … }
  6. 6. ThePerlReview•www.theperlreview.com 6ThingsAbout6 # Program execution BEGIN { put "BEGIN, at compile time, ASAP" } CHECK { put "CHECK, at compile time, ALAP" } INIT { put "INIT, during main execution, ASAP" } END { put "END, during main execution, ALAP" } for 0 .. 3 -> $item { my Int $square = $item ** 2; # Block phasers ENTER { put "tENTER block" } LEAVE { put "tLEAVE block" } KEEP { put "KEEP block: Got value $_" } # NYI UNDO { put "ttUNDO block" } PRE { put "PRE block ------" } POST { put "POST block" } # Loop phasers FIRST { put "tFIRST loop" } NEXT { put "tNEXT loop" } LAST { put "tLAST loop"; put "**** LOOP is done ****" } } https://www.learningperl6.com/2016/11/30/quick-tip-15-phasers/
  7. 7. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Sets
  8. 8. ThePerlReview•www.theperlreview.com 6ThingsAbout6 $ perl6 > my $set = set( qw/ 5 cat dog 37 / ) set(37, 5, cat, dog) > "5" ∈ $set True > 5 ∈ $set False > "5" (elem) $set True
  9. 9. ThePerlReview•www.theperlreview.com 6ThingsAbout6 $ perl6 > my $set_a = set qw/ a b c /; set(a, c, b) > my $set_b = set qw/ c d e /; set(c, e, d) > $set_a ∪ $set_b set(a, c, b, e, d) > $set_a ∩ $set_b set(c) > $set_a ∖ $set_b set(a, b) > $set_a ⊖ $set_b set(a, b, e, d)
  10. 10. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Sequences
  11. 11. ThePerlReview•www.theperlreview.com 6ThingsAbout6 1, 2 … 10 # 1 to 10 1, 3 … 10 # odds 1, 5 … 10 # 1,5,9 1, 2 … * # infinite, lazy 1, 3 … * # odds 1, 5 … * # 1,5,9 1, 1, -> $n,$m {$n+$m} … * 1, 1, { $^a + $^b } … * 1, 1, * + * … *
  12. 12. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Types
  13. 13. ThePerlReview•www.theperlreview.com 6ThingsAbout6 > my Int $i = 137 137 > my Int $j = 22/7 Type check failed in assignment to $j; expected Int but got Rat (<22/7>)
  14. 14. ThePerlReview•www.theperlreview.com 6ThingsAbout6 > my Int @array = ( 1, 2, 3, 4 ) [1 2 3 4] > my Int @array = qw/ 1 2 3 4 / Type check failed in assignment to @array; expected Int but got Str ("1")
  15. 15. ThePerlReview•www.theperlreview.com 6ThingsAbout6 > my Int %hash{Str} {} > %hash{ "Butterflies" } = 137 137 > %hash{ "Llamas" } = "Huh?" Type check failed in binding to assignval; expected Int but got Str ("Huh?") > %hash{ 5 } = 1 Type check failed in binding to key; expected Str but got Int (5)
  16. 16. ThePerlReview•www.theperlreview.com 6ThingsAbout6 my package EXPORT::DEFAULT { ... subset Pos of Real where * > 0; subset Neg of Real where * < 0; subset Zero of Real where * == 0; subset UNumeric of Real where * >= 0; subset Even of Int where * % 2 == 0; subset Odd of Int where * % 2; } https://github.com/bradclawsie/Subsets-Common
  17. 17. ThePerlReview•www.theperlreview.com 6ThingsAbout6 subset ZInt of Cool is export where { state ( $min, $max ) = %names.keys.sort( { $^a <=> $^b } ).[0,*-1]; ( $_.truncate == $_ and $min <= $_ <= $max ) or note "Expected a known atomic number between $min and $max" and False; }; https://github.com/briandfoy/perl6-chemistry-elements
  18. 18. ThePerlReview•www.theperlreview.com 6ThingsAbout6 MAIN
  19. 19. ThePerlReview•www.theperlreview.com 6ThingsAbout6 sub MAIN ( $n, $m ) { put "N: $n M: $m"; } $ perl6 main.p6 Usage: main.p6 <n> <m>
  20. 20. ThePerlReview•www.theperlreview.com 6ThingsAbout6 sub MAIN ( $n, $m = 5 ) { put "N: $n M: $m"; } $ perl6 main.p6 Usage: main.p6 <n> [<m>]
  21. 21. ThePerlReview•www.theperlreview.com 6ThingsAbout6 sub MAIN ( Int $n, Int $m ) { put "N: $n M: $m"; } sub MAIN ( Int $n where * > 0, Int $m where * < 0 ) { put "N: $n M: $m"; }
  22. 22. ThePerlReview•www.theperlreview.com 6ThingsAbout6 multi MAIN ( Int $n where * > 0, Int $m where * < 0 ) { put "PosNeg => N: $n M: $m"; } multi MAIN ( Int $n where * > 0, Int $m where * > 0 ) { put "PosPos => N: $n M: $m"; }
  23. 23. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Grammars
  24. 24. ThePerlReview•www.theperlreview.com 6ThingsAbout6 grammar Legal::Module::Name { token TOP { ^ <identifier> [<separator><identifier>] ** 0..* $ } token identifier { # leading alpha or _ only <[A..Za..z_]> <[A..Za..z0..9]> ** 0..* } token separator { :: # colon pairs } } my $match_obj = Legal::Module::Name.parse($s); http://perltricks.com/article/144/2015/1/13/How-to-create-a-grammar-in-Perl-6/
  25. 25. ThePerlReview•www.theperlreview.com 6ThingsAbout6 Questions

×