6 more
things
about 6
Boston.pm, 10 Jan 2017
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
perlmodules.net
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Rats
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
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
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
$ perl6
To exit type 'exit' or '^D'
> 1/3 + 1/4
0.583333
> (1/3 + 1/4).denominator
12
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Soft Failures
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
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Resume
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
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
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Interpolation
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
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
$_ = 'Hamadryas';
put "scalar: { $_ }";
put "scalar: { 1 + 2 }";
put "scalar: { .^name }";
scalar: Hamadryas
scalar: 3
scalar: Str
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
fmt
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
my $s = <22/7>;
put $s.fmt( '%5.4f' );
put $s.fmt( '%.14f' );
3.1429
3.14285714285714
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
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Lists of Lists
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
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
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
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
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
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
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]]
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 )
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 )
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 )
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 )
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 )
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 )
ThePerlReview•www.theperlreview.com
6MoreThingsAbout6
Questions

6 more things about Perl 6