25
6 things about 6 NY.pm, 8 Dec 2016

6 things about perl 6

Embed Size (px)

Citation preview

Page 1: 6 things about perl 6

6 thingsabout 6

NY.pm, 8 Dec 2016

Page 2: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6 perlmodules.net

Page 3: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Phasers

Page 4: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

count = 0;while ( … ) {

…}

output "There were {count} items!";

Page 5: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

use v6;

while( … ) {state $count = 0;LAST { put "There were $count" }…}

Page 6: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

# Program executionBEGIN { 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 phasersENTER { put "\tENTER block" }LEAVE { put "\tLEAVE block" }KEEP { put "KEEP block: Got value $_" } # NYIUNDO { put "\t\tUNDO block" }PRE { put "PRE block ------" }POST { put "POST block" }

# Loop phasersFIRST { 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/

Page 7: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Sets

Page 8: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

$ perl6> my $set = set( qw/ 5 cat dog 37 / )set(37, 5, cat, dog)> "5" ∈ $setTrue> 5 ∈ $setFalse> "5" (elem) $setTrue

Page 9: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

$ 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_bset(a, c, b, e, d)> $set_a ∩ $set_bset(c)> $set_a ∖ $set_bset(a, b)> $set_a ⊖ $set_bset(a, b, e, d)

Page 10: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Sequences

Page 11: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 61, 2 … 10 # 1 to 101, 3 … 10 # odds1, 5 … 10 # 1,5,9

1, 2 … * # infinite, lazy1, 3 … * # odds1, 5 … * # 1,5,9

1, 1, -> $n,$m {$n+$m} … *1, 1, { $^a + $^b } … * 1, 1, * + * … *

Page 12: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Types

Page 13: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

> my Int $i = 137137> my Int $j = 22/7Type check failed in assignment to $j; expected Int but got Rat (<22/7>)

Page 14: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

> 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")

Page 15: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

> my Int %hash{Str}{}> %hash{ "Butterflies" } = 137137> %hash{ "Llamas" } = "Huh?"Type check failed in binding to assignval; expected Int but got Str ("Huh?")

> %hash{ 5 } = 1Type check failed in binding to key; expected Str but got Int (5)

Page 16: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

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

Page 17: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

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

Page 18: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

MAIN

Page 19: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

sub MAIN ( $n, $m ) {put "N: $n M: $m";}

$ perl6 main.p6Usage: main.p6 <n> <m>

Page 20: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

sub MAIN ( $n, $m = 5 ) {put "N: $n M: $m";}

$ perl6 main.p6Usage: main.p6 <n> [<m>]

Page 21: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

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";}

Page 22: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

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";}

Page 23: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Grammars

Page 24: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

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/

Page 25: 6 things about perl 6

The Perl Review • www.theperlreview.com

6 Things About 6

Questions