use feature ‘say’;
say “This is a test!”;
{
no feature ‘say’;
say “This is fatal!”;
}
Slide 9
Slide 9 text
use 5.16.0;
say “This is a test!”;
{
no feature ‘say’;
say “This is fatal!”;
}
Slide 10
Slide 10 text
#!/usr/bin/perl
use strict;
use warnings;
use 5.16.0; # use feature ‘:5.16’;
my $x = Reticulator->new;
$x->reticulate(@splines);
Slide 11
Slide 11 text
#!/usr/bin/perl
use strict;
use warnings;
# no feature;
my $x = Reticulator->new;
$x->reticulate(@splines);
Slide 12
Slide 12 text
#!/usr/bin/perl
use strict;
use warnings;
# use feature ‘:default’
my $x = Reticulator->new;
$x->reticulate(@splines);
Slide 13
Slide 13 text
array_base: $[
Slide 14
Slide 14 text
Cool New Features!
Slide 15
Slide 15 text
perldiag
$str = “Greetings, $name. Your last
login was $last. It is now $time.”;
Better Error Message(s)
Slide 16
Slide 16 text
perldiag
$str = “Greetings, $name. Your last
login was $last. It is now $time.”;
Better Error Message(s)
Use of uninitialized value in
concatenation (.) or string at
hello.plx line 9.
Slide 17
Slide 17 text
perldiag
Better Error Message(s)
Use of uninitialized value $time in
concatenation (.) or string at
hello.plx line 9.
$str = “Greetings, $name. Your last
login was $last. It is now $time.”;
Slide 18
Slide 18 text
perlsub
my $LINES_READ = 0;
sub read_line {
$LINES_READ++;
...
}
State Variables
Slide 19
Slide 19 text
perlsub
{
my $LINES_READ = 0;
sub read_line {
$LINES_READ++;
...
}
}
State Variables
Slide 20
Slide 20 text
perlsub
sub read_line {
state $LINES_READ = 0;
$LINES_READ++;
...
}
State Variables
Slide 21
Slide 21 text
perlop
truth and definedness
Slide 22
Slide 22 text
perlop
truth and definedness
sub record_sale {
Slide 23
Slide 23 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
Slide 24
Slide 24 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
Slide 25
Slide 25 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
|| $product->price;
Slide 26
Slide 26 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
|| $product->price;
...
Slide 27
Slide 27 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
|| $product->price;
...
}
Slide 28
Slide 28 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
$price = defined $amount
? $amount
: $product->price;
...
}
Slide 29
Slide 29 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
|| $product->price;
...
}
Slide 30
Slide 30 text
perlop
truth and definedness
sub record_sale {
my ($product, $amount) = @_;
my $price = $amount
// $product->price;
...
}
Slide 31
Slide 31 text
perlop
the new OR operator
sub record_sale {
my ($product, $amount) = @_;
$amount //= $product->cost;
...
}
Slide 32
Slide 32 text
perlfunc
- new built-in, say
- it’s like print
- but it adds a newline for you
say $what
Slide 33
Slide 33 text
perlfunc
say $what
Slide 34
Slide 34 text
perlfunc
say $what
print “Hello, world!\n”;
Slide 35
Slide 35 text
perlfunc
say $what
print “Hello, world!\n”;
print “$message\n”;
Slide 36
Slide 36 text
perlfunc
say $what
print “Hello, world!\n”;
print “$message\n”;
print “$_\n” for @lines;
Slide 37
Slide 37 text
perlfunc
say $what
print “Hello, world!\n”;
print “$message\n”;
print “$_\n” for @lines;
say “Hello, world!”;
Slide 38
Slide 38 text
perlfunc
say $what
print “Hello, world!\n”;
print “$message\n”;
print “$_\n” for @lines;
say “Hello, world!”;
say $message;
Slide 39
Slide 39 text
perlfunc
say $what
print “Hello, world!\n”;
print “$message\n”;
print “$_\n” for @lines;
say “Hello, world!”;
say $message;
say for @lines;
Slide 40
Slide 40 text
No content
Slide 41
Slide 41 text
$ perl -e ‘print “Foo\n”’
Slide 42
Slide 42 text
$ perl -e ‘print “Foo\n”’
$ perl -E ‘say “Foo”’
Slide 43
Slide 43 text
sub fact {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * fact($x - 1);
}
Recursion!
Slide 44
Slide 44 text
sub fact {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * fact($x - 1);
}
Recursion!
Slide 45
Slide 45 text
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
Recursion!
Slide 46
Slide 46 text
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
Recursion!
Slide 47
Slide 47 text
my $fact;
$fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
Recursion!
Slide 48
Slide 48 text
my $fact;
$fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
Recursion!
Slide 49
Slide 49 text
use Scalar::Util qw(weaken);
my $fact = do {
my $f1;
my $f2 = $f1 = sub {
my ($x) = @_;
return $x if $x == 1;
return $x * $f1->($x - 1);
};
weaken($f1);
$f1;
};
Recursion!
Slide 50
Slide 50 text
use 5.16.0; # current_sub
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * __SUB__->($x - 1);
};
Recursion!
Slide 51
Slide 51 text
Filehandles!
Slide 52
Slide 52 text
autodie
autodie
open my $fh, ‘<‘, $filename;
while (<$fh>) {
...
}
close $fh;
Slide 53
Slide 53 text
autodie
autodie
open my $fh, ‘<‘, $filename
or die “couldn’t open $filename: $!”;
while (<$fh>) {
...
}
close $fh
or die “couldn’t close $filename: $!”;
Slide 54
Slide 54 text
autodie
autodie
use autodie;
open my $fh, ‘<‘, $filename;
while (<$fh>) {
...
}
close $fh;
Slide 55
Slide 55 text
autodie
autodie
use autodie;
open my $fh, ‘<‘, $filename;
while (<$fh>) {
no autodie;
rmdir or warn “couldn’t remove $_: $!”;
}
close $fh;
Slide 56
Slide 56 text
autodie
autodie
use autodie;
sub foo {
my $filename = shift;
open my $fh, ‘<‘, $filename;
while (<$fh>) {
...
}
} # this implicit close DID NOT AUTODIE
Slide 57
Slide 57 text
perlopentut
IO::File
sub stream_to_fh {
my ($self, $fh) = @_;
fileno $fh
or die “can’t stream to closed fh”;
while (my $hunk = $self->next_hunk) {
print {$fh} $hunk;
}
close $fh or die “error closing: $!”;
}
Slide 58
Slide 58 text
perlopentut
IO::File
sub stream_to_fh {
my ($self, $fh) = @_;
$fh->fileno
or die “can’t stream to closed fh”;
while (my $hunk = $self->next_hunk) {
$fh->print($hunk);
}
$fh->close or die “error closing: $!”;
}
Slide 59
Slide 59 text
perlopentut
IO::File
sub stream_to_fh {
...
$fh->print($hunk);
...
$fh->close or die “error closing: $!”;
}
open my $target, ‘>’, ‘/dev/null’
or die “can’t open bit bucket: $!”;
stream_to_fh($target);
Slide 60
Slide 60 text
perlopentut
IO::File
use IO::File;
sub stream_to_fh {
...
$fh->print($hunk);
...
$fh->close or die “error closing: $!”;
}
open my $target, ‘>’, ‘/dev/null’
or die “can’t open bit bucket: $!”;
stream_to_fh($target);
Slide 61
Slide 61 text
perlopentut
IO::File
use 5.14.0;
sub stream_to_fh {
...
$fh->print($hunk);
...
$fh->close or die “error closing: $!”;
}
open my $target, ‘>’, ‘/dev/null’
or die “can’t open bit bucket: $!”;
stream_to_fh($target);
Slide 62
Slide 62 text
perlopentut
IO::File
use 5.14.0;
use autodie;
sub stream_to_fh {
...
$fh->print($hunk);
...
$fh->close or die “error closing: $!”;
}
open my $target, ‘>’, ‘/dev/null’
or die “can’t open bit bucket: $!”;
stream_to_fh($target);
perldoc
overloading
- the -x overload
- the qr overload
- "no overloading"
- unknown overload warns
Slide 68
Slide 68 text
Other New Features!
Slide 69
Slide 69 text
smrt match
Slide 70
Slide 70 text
if ($x ~~ $y) {
...
}
smrt match
Slide 71
Slide 71 text
perldoc
smrt match
Slide 72
Slide 72 text
perldoc
smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
Slide 73
Slide 73 text
perldoc
smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
- and some of them redispatch recursively
Slide 74
Slide 74 text
perldoc
smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
- and some of them redispatch recursively
- no, you won't remember them all
Slide 75
Slide 75 text
perldoc
smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
- and some of them redispatch recursively
- no, you won't remember them all
- ...and they can't be intuited
Slide 76
Slide 76 text
Matching
Slide 77
Slide 77 text
if ($x ~~ $y) {...}
Matching
Slide 78
Slide 78 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
Matching
Slide 79
Slide 79 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
if ($str ~~ @arr) {...}
Matching
Slide 80
Slide 80 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
if ($str ~~ @arr) {...}
if ($str ~~ [ \%h, ...]) {...}
Matching
Slide 81
Slide 81 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
if ($str ~~ @arr) {...}
if ($str ~~ [ \%h, ...]) {...}
if (%hash ~~ %h) {...}
Matching
Slide 82
Slide 82 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
if ($str ~~ @arr) {...}
if ($str ~~ [ \%h, ...]) {...}
if (%hash ~~ %h) {...}
if (%hash ~~ @arr) {...}
Matching
Slide 83
Slide 83 text
if ($x ~~ $y) {...}
if ($str ~~ %hash) {...}
if ($str ~~ @arr) {...}
if ($str ~~ [ \%h, ...]) {...}
if (%hash ~~ %h) {...}
if (%hash ~~ @arr) {...}
if (%hash ~~ [ \%h,...]) {...}
Matching
Slide 84
Slide 84 text
given ($x) {
when ($y) {
...
}
when ($z) {
...
}
}
Slide 85
Slide 85 text
given ($x) {
when ($y) {
try { ... }
catch {
warn “error: $_”;
return undef;
}
}
}
Slide 86
Slide 86 text
each @array
Slide 87
Slide 87 text
while (my ($i, $v) = each @array) {
say “$i: $v”;
}
each @array
perlunicode
Perl 5.16 is Better
- Unicode 6.1
- every character property is available
Slide 122
Slide 122 text
perlunicode
Perl 5.16 is Better
- Unicode 6.1
- every character property is available
- \X in regex is more sensible
Slide 123
Slide 123 text
perlunicode
“The Unicode Bug”
Slide 124
Slide 124 text
perlunicode
“The Unicode Bug”
- strings aren’t always treated as Unicode
Slide 125
Slide 125 text
perlunicode
“The Unicode Bug”
- strings aren’t always treated as Unicode
- this causes weird bugs that take ages to find
Slide 126
Slide 126 text
perlunicode
“The Unicode Bug”
- strings aren’t always treated as Unicode
- this causes weird bugs that take ages to find
- use feature ‘unicode_strings’;
Slide 127
Slide 127 text
perlunicode
“The Unicode Bug”
- strings aren’t always treated as Unicode
- this causes weird bugs that take ages to find
- use feature ‘unicode_strings’;
- or use 5.12.0
Slide 128
Slide 128 text
perldoc
Unicode eval
- eval $str
- is that octets or chars?
- what if it includes "use utf8"
- or you're under "use utf8"?
perlre
New Regex Modifiers
# To be really ASCII-only:
die “funny un-American characters”
if $str =~ /\P{ASCII}/;
$str =~ /...actual pattern.../;
Slide 190
Slide 190 text
study
Slide 191
Slide 191 text
my $re = qr{...complex...};
study
Slide 192
Slide 192 text
my $re = qr{...complex...};
my $str = q{...long complex...};
study
Slide 193
Slide 193 text
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
study
Slide 194
Slide 194 text
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
study $str; # does stuff
study
Slide 195
Slide 195 text
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
study $str; # does stuff
$str =~ $re; # fast!!
study
Slide 196
Slide 196 text
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow but right!!
study $str; # does stuff
$str =~ $re; # who knows!!
study
Slide 197
Slide 197 text
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow but right!!
study $str; # does nothing
$str =~ $re; # slow but right!!
study