Perl 6
One-Liners
Andrew Shitov


German Perl Workshop

Munich, 8 March 2019
A PLAY IN EIGHT ACTS
Rakudo
command-line
options1
-e
execute
$ perl6 -e'say 42'
$ perl6 -e'$*PERL.version.say'
v6.d
-e, not -E
-n
repeat for each input line
$ perl6 -ne'say [+] .split(" ")' 
data.txt
-p
repeat and print
$ perl6 -npe'.=flip' data.txt
Perl 6 am MAIN
2
multi sub MAIN() {
say "No args"
}
multi sub MAIN($x) {
say "Single arg $x"
}
multi sub MAIN($x, $y) {
say "Two args: $x and $y"
}
$ perl6 main.pl
No args
$ perl6 main.pl 1
Single arg 1
$ perl6 main.pl 1 2
Two args: 1 and 2
$ perl6 main.pl 1 2 3
Usage:
main.pl <x> <y>
multi sub MAIN(Int $x) {
say "Integer $x"
}
multi sub MAIN(Str $s) {
say "String $s"
}
$ perl6 main2.pl abc
String abc
$ perl6 main2.pl 42
Ambiguous call to 'MAIN(IntStr)';
these signatures all match:
:(Int $x)
:(Str $s)
in block <unit> at main2.pl line 5
multi sub MAIN(IntStr $x) {
say "Integer $x"
}
multi sub MAIN(Str $s) {
say "String $s"
}
multi sub MAIN(*@args) {
say "Passed {@args.elems} args"
}
$ perl6 main3.pl
Passed 0 args
$ perl6 main3.pl 1
Passed 1 args
$ perl6 main3.pl a b c d
Passed 4 args
Mangling

Text

Files3
8 exercises
$ perl6 -npe's/$/n/' text.txt
$ perl6 -npe's/$/n/' text.txt
Double-space a file
$ perl6 -ne'.say if .chars' text.txt
$ perl6 -ne'.say if .chars' text.txt
Remove all blank lines
$ perl6 -ne'.say if /S/' text.txt
$ perl6 -ne'.say if /S/' text.txt
Remove all blank lines
docs.perl6.org/language/variables#The_$_variable
$ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
Number all lines in a file
$ perl6 -ne'say ++$ ~ ". " ~ $_' text.txt
$ perl6 -npe'.=uc' text.txt
Convert all text to uppercase
$ perl6 -npe'.=uc' text.txt
$ perl6 -npe'.=trim' text.txt
Strip whitespaces
$ perl6 -npe'.=trim' text.txt
$ perl6 -ne'.say ; exit' text.txt
Print the first line of a file
$ perl6 -ne'.say ; exit' text.txt
$ perl6 -npe'exit if $++ == 10' text.txt
Print the first 10 lines of a file
$ perl6 -npe'exit if $++ == 10' text.txt
Reverse a file
.say for lines.reverse
$ perl6 reverse.pl < text.txt
Reverse a file
.say for $*IN.lines.reverse
$ perl6 reverse.pl < text.txt
Reverse a file
.say for
@*ARGS[0].IO.open.lines.reverse
$ perl6 reverse.pl text.txt
Reading from multiple files
.say for $*ARGFILES.lines
$ perl6 work.pl a.txt b.txt
Batch file renaming
@*ARGS[0..*-2].sort.map:
*.Str.IO.rename(++@*ARGS[*-1])
$ perl6 rename.pl *.jpg img_0000.jpg
Perl 6

Meta-
Operators4
Compute totals
put [Z+] lines.map: *.words
Merge into columns
.say for [Z~] @*ARGS.map: *.IO.lines;
$ perl6 merge.pl a.txt b.txt
Product table
1..10 X* 1..10
(1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21
24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6
12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32
40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70
80 90 100)
Factorial
say [*] 1..2019
Least common multiplier
say [lcm] 1..20
Rotate a matrix
[Z] <A B C>, <D E F>, <H I J>
((A D H) (B E I) (C F J))
Sequence

Operator

. . .5
Fibonacci numbers
0, 1, * + * ... *
Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
First 30 elements
Prime numbers
say ((1..*).grep: *.is-prime)[10000]
The 10001 prime numberst
Prime numbers
.say if .is-prime for ^100
Prime numbers below 100
The value of π
say π
The value of π
say pi
The value of π
say 4 * [+] (^1000).map({(-1) ** $_ / (2 * $_ + 1)})
The value of π
say 4 * [+] (1, -1, 1 … *) «/« 

(1, 3, 5 … 9999);
Area of a square
say π × $𝜌²
Generating

Random

Numbers6
Random password
('0'..'z').pick(15).join.say
Non-repeating characters
Random password
('0'..'z').roll(15).join.say
Characters may repeat
('0'..'z').join.say
0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`
abcdefghijklmnopqrstuvwxyz
Random integer
2019.rand.Int.say
0 to 2019
Random integer
2019.rand.rand.Int.say
Is this number more random? :-)
Solving
Euler
Problems7
1. Print the sum of all multiples

of 3 and 5 below 1000
say sum((1..999).grep: * %% (3 | 5))
1. Print the sum of all multiples

of 3 and 5 below 1000
sub f($n) {
($n <<*>> (1...1000 / $n)).grep: * < 1000
}
say (f(3) ∪ f(5)).keys.sum;
My initial solution :-)
2. Print the sum of all even

Fibonacci numbers 

below 4,000,000
(1, 1, * + * ...^ * >
4_000_000).grep(* %% 2).sum.say
3. Find the largest palindromic
number, which is a product

of two three-digit numbers.
(((999...100) X* (999...100)).grep:
{$^a eq $^a.flip}).max.say
3. Find the largest palindromic
number, which is a product

of two three-digit numbers.
(((999...100) X* (999...100)).grep:

{$^a eq $^a.flip}).head(10).max.say
4. Print the sum

of big numbers
4. Print the sum

of big numbers
<
371072874339021027987979982208375902465101357402
# Other 98 numbers here
535035345264725242508740540755917897812643303316
>.sum.substr(0, 10).say
19. Count Sundays between

the two dates
say (
Date.new(year => 1901) ..^ Date.new(year => 2001)
).grep({.day == 1 && .day-of-week == 7}).elems
19. Count Sundays between

the two dates
say (
Date.new(year => 1901, month => 1, day => 1) ..
Date.new(year => 2000, month => 12, day => 31)
).grep(*.day == 1).grep(*.day-of-week == 7).elems
19. Count Sundays between

the two dates
say +(1901..2000 X 1..12).map(
{Date.new(|@_, 1)}
).grep(*.day-of-week == 7);
34. Print the sum of all numbers,
which are equal to the sum
of factorials of their digits
say [+] (3..50_000).grep(

{$_ == [+] .comb.map({[*] 2..$_})})
Perl 6

Golf
Contest8
First prime numbers
say ((1..*).grep: *.is-prime)[10000]
First prime numbers
say ((1..*).grep: *.is-prime)[10000]
.is-prime&&.say for ^Ⅽ
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
Side effects!

Not recommended!
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>514229)».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
(0,1,*+*…*> )».say
First 30 Fibonacci numbers
.say for (0, 1, * + * ... *)[^31]
.say for (0,1,*+*...*)[^31]
(0,1,*+*...*)[^31]>>.say
(0,1,*+*…*)[^31]».say
(0,1,*+*…*>7⁷)».say
(0,1,*+*…*> )».say
(0,1,*+*…/20/)».say
Andrew Shitov


German Perl Workshop

Munich, 7 March 2019
github.com/ash
slideshare.net/andy.sh

Perl6 one-liners