Passionate programmers have discovered how coding in Perl Six can be playful, imaginative, pleasurable and exhilarating.
This talk includes tasteful illustrations for curious monoglot and polyglot programmers alike.
Discover the Joy of Six!
26. Comb the Logs
use IP::Address;
my $logs = 'Check these version 4 IPs: 213.120.160.146,
12.10.12.13';
my @ips = $logs.comb(/<IP::Address::v4>/);
IPv6 too?
http://rosettacode.org/wiki/Parse_an_IP_Address#Perl_6
27. Rosetta Stone Code
A+ to ZED
596 Languages
Hundreds of Tasks
Where Larry Wall has
O(fun)
http://rosettacode.org
29. Whipupitude
// Java
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal
System.out.println("Hello, World");
}
}
# Perl 6
say 'Hello, World';
'Hello, World'.say;
30. Whipupitude
#!/usr/bin/env perl6
#| deploy to a node by name
sub MAIN ($node-name) {
say "deploying now to node $node-name";
}
shell> ./deploy.p6 -help
Usage:
./deploy.p6 <node-name> -- deploy to a node by name
31. Whipupitude
use IP::Address;
#| deploy to a node by name
multi sub MAIN ($node-name) {
say "deploying to node: $node-name";
}
#| deploy to a specific IP address
multi sub MAIN ($node-ip where { $_ ~~ /<IP::Address::v4>/ }) {
say "deploying to ip address: $node-ip";
}
shell> ./deploy.p6 -help
Usage:
./deploy.p6 <node-name> -- deploy to a node by name
./deploy.p6 <node-ip> -- deploy to a specific IP address
32. Expressivity
# Perl5 old Skool
sub factorial {
my $n = shift;
my $result = 1;
for (my $i = 1; $i <= $n; ++$i) {
$result *= $i;
};
return $result;
}
33. Iterative / Recursive / Declarative?
Expressive
sub postfix:<!> { [*] 1 .. $^n }
say 5!; # prints 120
35. [Reduction Meta Operator]
my @numbers = 1 .. 3;
say [+] @numbers;
say [max] @numbers;
say [min] @numbers;
say [~] @numbers;
say [>] @numbers;
say [<] @numbers;
say [>] @numbers.reverse;
36. [Reduction Meta Operator]
my @numbers = 1 .. 3;
say [+] @numbers; # 6
say [max] @numbers; # 3
say [min] @numbers; # 1
say [~] @numbers; # 123
say [>] @numbers; # False
say [<] @numbers; # True
say [>] @numbers.reverse; # True
37. X product – Meta Operator
my @suits = <♣ ♢ ♡ ♠>;
my @ranks = flat 1..10,'J','Q','K','A';
my @deck = @ranks X~ @suits;
my @hand = @deck.pick(5);
say @hand;
shell> perl6 hand.p6
7♣ 5♡ 2♣ J♠ 10♠
38. Lean Coding ...
BOSS: “I need dates for all the last Fridays of the month for
this year”
Lean programmer grabs a highlighter pen and a calendar.
BOSS: “Oh, and for any year out of the last 20”
Lean programmer opens up a terminal window … and types ...
39. Python's Last Friday (14 LOC)
import calendar
c=calendar.Calendar()
fridays={}
year=raw_input("year")
for item in c.yeardatescalendar(int(year)):
for i1 in item:
for i2 in i1:
for i3 in i2:
if "Fri" in i3.ctime() and year in
i3.ctime():
month,day=str(i3).rsplit("-",1)
fridays[month]=day
40. Last Friday in Perl 6 (7 LOC)
sub MAIN (Int :$year = Date.today.year) {
my @fri;
for Date.new("$year-01-01") .. Date.new("$year-12-31"){
@fri[.month] = .Str if .day-of-week == 5;
}
.say for @fri[1..12];
}
shell> perl6 last-friday.p6 --help
Usage:
last-friday.p6 [--year=<Int>]
shell> perl6 last-friday.p6
2015-12-25