15. Better Error Message(s)
$str = “Greetings, $name. Your last
login was $last. It is now $time.”;
Use of uninitialized value in
concatenation (.) or string at
hello.plx line 9.
perldiag
16. Better Error Message(s)
$str = “Greetings, $name. Your last
login was $last. It is now $time.”;
Use of uninitialized value $time in
concatenation (.) or string at
hello.plx line 9.
perldiag
42. Recursion!
sub fact {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * fact($x - 1);
}
43. Recursion!
sub fact {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * fact($x - 1);
}
44. Recursion!
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
45. Recursion!
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
46. Recursion!
my $fact;
$fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
47. Recursion!
my $fact;
$fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * $fact->($x - 1);
};
48. Recursion!
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;
};
49. Recursion!
use 5.16.0; # current_sub
my $fact = sub {
my ($x) = @_; # must be +int
return $x if $x == 1;
return $x * __SUB__->($x - 1);
};
52. autodie
open my $fh, ‘<‘, $filename
or die “couldn’t open $filename: $!”;
while (<$fh>) {
...
}
close $fh
or die “couldn’t close $filename: $!”;
autodie
54. autodie
use autodie;
open my $fh, ‘<‘, $filename;
while (<$fh>) {
no autodie;
rmdir or warn “couldn’t remove $_: $!”;
}
close $fh;
autodie
55. autodie
use autodie;
sub foo {
my $filename = shift;
open my $fh, ‘<‘, $filename;
while (<$fh>) {
...
}
} # this implicit close DID NOT AUTODIE
autodie
56. 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: $!”;
}
perlopentut
57. 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: $!”;
}
perlopentut
58. 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);
perlopentut
59. 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);
perlopentut
60. 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);
perlopentut
61. 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);
perlopentut
71. smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
perldoc
72. smrt match
- if $x and $y are unknown, there are 23
possible dispatch paths
- and some of them redispatch recursively
perldoc
73. 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
perldoc
74. 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
perldoc
124. “The Unicode Bug”
- strings aren’t always treated as Unicode
- this causes weird bugs that take ages to find
perlunicode
125. “The Unicode Bug”
- strings aren’t always treated as Unicode
- this causes weird bugs that take ages to find
- use feature ‘unicode_strings’;
perlunicode
126. “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
perlunicode
127. Unicode eval
- eval $str
- is that octets or chars?
- what if it includes "use utf8"
- or you're under "use utf8"?
perldoc
191. study
my $re = qr{...complex...};
my $str = q{...long complex...};
192. study
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
193. study
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
study $str; # does stuff
194. study
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow!!
study $str; # does stuff
$str =~ $re; # fast!!
195. study
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow but right!!
study $str; # does stuff
$str =~ $re; # who knows!!
196. study
my $re = qr{...complex...};
my $str = q{...long complex...};
$str =~ $re; # slow but right!!
study $str; # does nothing
$str =~ $re; # slow but right!!