Perl6 is a powerful programming language that incorporates many programming paradigms including functional, object-oriented, and reactive programming. It has built-in support for concepts like promises to handle asynchronous code, channels for communicating between asynchronous processes, and sets/bags for storing unique values. The language also has features for rational numbers, lazy evaluation, roles for composition, and easy integration with native libraries. Perl6 aims to provide programmers a flexible toolbox to build applications in many different styles.
2. Obligatory About Me Slide
Hi.
I’m Simon. (Also known as Scimon in many places)
I write Perl and when I can Perl6.
I’ve been doing it for 15 years and a bit. (Not the Perl6… well)
Now working at Zoopla. We’re hiring, (not sure where we’ll put you…)
Wrote Timer::Breakable, writing more modules.
3. A Disclaimer
I am still learning Perl6.
It’s a massive powerful language with a lot of scope to create amazing stuff.
Some of the things I say today may be wrong.
This will be because I still don’t fully understand some of the really neat things I’m
playing with.
But all the code examples I give work!
I tested them all.
4. Humourous Caption Slide
The Year of Linux on the Desktop!
1998, 1999, 2000, 2001, 2002 …
The Year of Perl6 in Production ?
2018...
5. Multiple Programming Paradigms
What’s your poison?
● Functional Programming ?
● Object Orientated ?
● Reactive or Event Based ?
● Strongly Typed ?
● Weakly Typed ?
Perl6 builds in the concept of the swiss army chainsaw and gives you a toolbox
from which you can build whatever chainsaw you want.
6. List::Util
● reduce : [+] @a or @a.reduce(*+*)
● any : so any(@a) > 10
● all : so all(@a) > 10
● none : so none(@a) > 10
● first : @a.first( * > 10 )
● max : @a.max
● maxstr : @a.max( *.Str )
● min : @a.min
● minstr : @a.min( *.Str )
● product : [*] @a or @a.reduce(* * *)
● sum : @a ?? @a.sum !! Nil
● sum0 : @a.sum
● shuffle : @a.pick(@a)
● uniq : @a.unique
● uniqnum : @a.unique( :as( *.Num ) )
● uniqstr : @a.unique( :as( *.Str ) )
Perl6 has Pairs as a Type making the pair methods redundant.
Note that Perl6 is typed and casting a string to a number may raise an exception.
13. Promises
“Asynchronous programming for mortals” or “No more callback hell”
my $p1 = start { sleep 3; print “Or a simple start? ”; };
my $p2 = Promise.in(2).then( { print “Why not use a timer? ” } );
my $p3 = Promise.anyof( $p1, $p2 ).then( { print “Something is done. ” } );
my $p4 = Promise.allof( $p1, $p2, $p3 ).then( { print “All the promises done.” } );
print “Promises Begun… ”;
await( $p1, $p2, $p3, $p4 );
say “All done.”;
Promises Begun… Why not use a timer? Something is done. Or a simple start? All the
promises done. All done.
With some pauses…
14. Channels and Supplies
constant children = 8;
sub MAIN( Str $text ) {
my $dir-channel = Channel.new();
my $file-channel = Channel.new();
my @readers;
for (^8) -> $idx {
@readers[$idx] = Promise.new();
my $sup = $file-channel.Supply.map( -> $path {
my @a = $path.lines.grep( {
defined $_.index($text)
} ); $path => @a
} ).tap( -> $data {
for $data.value { say("{$data.key} : {$_}") }
}, done => { @readers[$idx].keep } );
}
for dir( "." ) -> $p { $dir-channel.send($p) };
loop {
if $dir-channel.poll -> $path {
if ( $path.d ) {
$dir-channel.send( $_ )
for dir( $path )
} elsif ( $path.f ) {
$file-channel.send( $path );
}
} else {
$dir-channel.close;
$file-channel.close;
last;
}
}
await( @readers );
}
15. Channels and Supplies (Feeds : Secret Sauce)
my $sup = $file-channel.Supply
.map( -> $path {
my @a = $path.lines.grep( {
defined $_.index($text)
} ); $path => @a
} )
.tap( -> $data {
for $data.value { say("{$data.key} : {$_}") }
}, done => { @readers[$idx].keep } );
16. Sequences, Lazy Evaluation and Rational Numbers
my @primes = (1..*).grep( *.is-prime );
my @evens = 2,4,6...*;
my @fib = 1, 1, * + * ... *;
my $div0 = 42 / 0;
say $div0.nude; # NU(merator and) DE(nominator)
(42 0)
say 0.2 + 0.1 - 0.3 == 0; True
17. Sets and Bags
my @primes = (1..*).grep( *.is-prime );
my @fib = 1,2,*+*...*;
my $prime-set = set( @primes[0..50] );
say $_, " prime? ", $_ ∈ $prime-set
for @fib[0..5];
say $prime-set ∩ @fib[0..10];
(elem) and ∈ are synonyms as are (&) and ∩
Note : Set operators auto coerce their args to
Sets.
1 prime? False
2 prime? True
3 prime? True
5 prime? True
8 prime? False
13 prime? True
set(13 2 3 5 89)
19. NativeCall (A peek inside)
method write_to_png(Str $filename)
returns int32
is native($cairolib)
is symbol('cairo_surface_write_to_png') {*}
method rectangle(num64 $x, num64 $y, num64 $w, num64 $h)
is native($cairolib)
is symbol('cairo_rectangle') {*}
That simple. Here $cairolib is either 'libcairo-2' or ('cairo', v2)
depending on the architecture.
20. Roles
Yeah you know roles right? Well what about this?
role logger {
method log-msg ( Str $msg ) { say “{$msg} : {self.perl}” }
};
my $num = 5;
$num.?log-msg( "Not a logger" );
$num does logger;
$num.?log-msg( "Loggable" );
Or
my $result = 0 but True;
21. Unicode
my $á1 = “c[LATIN SMALL LETTER A WITH ACUTE]”; # Smart Quotes!
my $á2 = “ax301”;
say “$á1 : $á2”;
á : á
say $á1 eq á2;
True
say “$á1 : $á2”.uc;
Á : Á
my $æ = 2;
$æ = ( $æ * ¾ )²;
say “æ => $æ”;
æ => 2.25
22. All the other stuff
● Grammars
● Imaginary Numbers
● Proper Exceptions
● CPAN
● Meta Objects
● Telemetry
● JVM
● RakudoJS
● IO::Notification
● Date and DateTime built in
● (And so much more)
23. Further Reading (and Viewing)
● Perl 6 Docs - https://docs.perl6.org/
● High End Unicode in Perl 6 - https://youtu.be/Oj_lgf7A2LM
● Perl6 Superglue for the 21st Century - https://www.youtube.com/watch?v=q8stPrG1rDo
● Think Perl 6 - http://greenteapress.com/wp/think-perl-6/
● Using Perl 6 - https://deeptext.media/using-perl6
● Learning Perl 6 (On the Way) - https://www.learningperl6.com/
● Cro - http://cro.services/
● Bailador - https://github.com/Bailador/Bailador
● Sparrowdo - https://github.com/melezhik/sparrowdo
● Spitsh - https://github.com/spitsh/spitsh
● Roles vs Inheritance - https://www.youtube.com/watch?v=cjoWu4eq1Tw