6 more things about perl 6
TRANSCRIPT
The Perl Review • www.theperlreview.com
6 More Things About 6
perlmodules.net
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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]]
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 )
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 )
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 )
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 )
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 )
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 )