Top Banner
6 more things about 6 Boston.pm, 10 Jan 2017
33

6 more things about Perl 6

Apr 15, 2017

Download

Technology

_brian d _foy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 6 more things about Perl 6

6 morethings

about 6Boston.pm, 10 Jan 2017

Page 2: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Page 3: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

perlmodules.net

Page 4: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Rats

Page 5: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

$ perl -le 'print 0.3 - 0.2- 0.1'-2.77555756156289e-17

$ perl6 -e 'put 0.3 - 0.2 - 0.1'0

Page 6: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

$ perl6To exit type 'exit' or '^D'> 0.10.1> 0.1.^nameRat> 0.1.numerator1> 0.1.denominator10

Page 7: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

$ perl6To exit type 'exit' or '^D'> 1/3 + 1/40.583333> (1/3 + 1/4).denominator12

Page 8: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Soft Failures

Page 9: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

CATCH {default { put "Caught something" }}

my $fh = open 'not-there'; # this failsput "Result is " ~ $fh.^name;

unless $fh { # $fh.sogiven $fh.exception {

put "failure: {.^name}: {.message}";}

}

Result is Failurefailure: X::AdHoc: Failed to open file

Page 10: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Resume

Page 11: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

{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

Page 12: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

{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 fileI’m still going

Page 13: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Interpolation

Page 14: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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: Hamadryasarray: Dog Cat Birdhash: a 1b 2c 3

Page 15: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

$_ = 'Hamadryas';

put "scalar: { $_ }";put "scalar: { 1 + 2 }";put "scalar: { .^name }";

scalar: Hamadryasscalar: 3scalar: Str

Page 16: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

fmt

Page 17: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my $s = <22/7>;

put $s.fmt( '%5.4f' );put $s.fmt( '%.14f' );

3.14293.14285714285714

Page 18: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my @s = 1 .. 8;@s

.map( (*/7).fmt('%.5f') )

.join( "\n" ).put;

0.142860.285710.428570.571430.714290.857141.000001.14286

Page 19: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Lists of Lists

Page 20: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my $scalar = ( 1, 2, 3 );# my $scalar = 1, 2, 3; # Nope!

put "scalar: $scalar";put "scalar: { $scalar.^name }";

scalar: 1 2 3scalar: List

Page 21: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my @array = ( ( 1, 2 ), qw/a b/ );

put "elems: { @array.elems }";put "@array[]";put "@array[0]";put "{ @array[0].^name }";

21 2 a b1 2List

Page 22: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my $x = ( 1, 2, 6 );some_sub( $x );sub some_sub ( $x ) {

put "x has { $x.elems }";}

x has 3

Page 23: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my Buf $buf = Buf.new( 0xDE, 0xAD, 0xBE, 0xEF );

for $buf.values -> $c {put "c is $c";}

c is 222c is 173c is 190c is 239

Page 24: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my Buf $buf = Buf.new( 0xDE, 0xAD, 0xBE, 0xEF );

for $buf.rotor(2) -> $c {put "c is $c";}

c is 222 173c is 190 239

Page 25: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 173word is DEADc is 190 239word is BEEF

Page 26: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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]]

Page 27: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 )

Page 28: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 )

Page 29: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 )

Page 30: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 )

Page 31: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

my @f = @ab;

@f = gather {while @f {

@f[0].elems == 1 ??take @f.shift

[email protected]( @f.shift.Slip )

}}

( a b c d e f a b c x y z w )

Page 32: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

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 )

Page 33: 6 more things about Perl 6

The Perl Review • www.theperlreview.com

6 More Things About 6

Questions