What's new in Perl 5.10
Lots of new things!
Lots.
Lots of new things!
- 5.8.0 : 2002-Jul-18
- 5.10.0 : 2007-Dec-18
5 years + 5 months of development
Defined Or
Standard Or
$a || $b
<=>
$a ? $a : $b
Defined Or
Defined Or
$a // $b
<=>
defined $a ? $a : $b
Defined Or
Defined Or
$a // $b
<=>
defined $a ? $a : $b
More concise
Faster
Defined Or
Defined Or
$a // $b
<=>
defined $a ? $a : $b
More concise
Faster
$a //= $b
say
say(...) is a shorter form of
print ..., "\n";
Switch
given ...
when ... [
default ... ]
given ($foo) {
when (/^abc/) { $abc = 1; }
when (/^def/) { $def = 1; }
when (/^xyz/) { $xyz = 1; }
default { $nothing = 1; }
}
Switch
given ...
when ... [
default ... ]
given ($foo) {
when (/^abc/) { $abc = 1; }
when (/^def/) { $def = 1; }
when (/^xyz/) { $xyz = 1; }
default { $nothing = 1; }
}
Much more flexible than the C-style switch
Switch
How does it work?
given (EXPR) aliases $_ to EXPR
Switch
How does it work?
given (EXPR) aliases $_ to EXPR
$ perl -E 'given ("hello") {say "$_ world"}'
hello world
Switch
when(BOOL_EXPR) evaluates the boolean expression. For example:
when ($_ gt "ab") { ... }
when (/^\d+$/ and $_ < 10) { ... }
Switch
In other cases,
when(EXPR) does a
smart match of $_ against EXPR.
It's equivalent to:
when ($_ ~~ EXPR) { ... }
Smart Match
New smart match operator
~~
Does What You Mean with almost anything.
Smart Match
given ($foo) {
...
when (undef) {
say q($foo is undefined);
}
...
}
Smart Match
given ($foo) {
...
when ([1,3,5,7,9]) {
say q(That's an odd number);
}
...
}
Smart Match
given ($foo) {
...
when (@array) {
say '$foo belongs to @array';
}
...
}
Smart Match
given ($foo) {
...
when (%hash) {
say '$foo is a key in %hash';
}
...
}
Smart Match
given ($foo) {
...
when (\&complicated_check) {
say 'complicated_check($foo) is happy';
}
...
}
Smart Match
Some edge cases with ~~ will be fixed in 5.10.1
$_ ~~ $object
Smart Match
Some edge cases with ~~ will be fixed in 5.10.1
$_ ~~ $object
Should fail when ~~ is not overloaded
State variable
Keep their value between function calls.
sub foo {
state $x = 2;
++$x;
}
say foo() for 1..3;
gives:
3
4
5
use feature
The new keywords can raise compatibility problems.
use feature 'say';
use feature 'state';
use feature 'switch'; # given, when, default
use feature
The new keywords can raise compatibility problems.
use feature ':5.10';
use feature
The new keywords can raise compatibility problems.
use 5.010;
use feature
The new keywords can raise compatibility problems.
perl -E 'say "hello"'
Lexical $_
No more side effects
sub badfunc {
while(<IN>) { ... }
}
{
my $_;
...
badfunc();
}
Lexical $_
No more side effects
sub goodfunc {
my $_; # better than "local $_"
while(<IN>) { ... }
}
Lexical $_
No more side effects
sub goodfunc {
my $_; # better than "local $_"
while(<IN>) { ... }
{
our $_;
... # the global $_ is accessible here
}
}
(_) prototype
Like ($), but defaults to $_
sub foo (_) { say "hello @_" }
foo("world");
$_ = "boys"; foo();
{ my $_ = "girls"; foo(); }
(_) prototype
Like ($), but defaults to $_
sub foo (_) { say "hello @_" }
foo("world");
$_ = "boys"; foo();
{ my $_ = "girls"; foo(); }
Allows to emulate built-ins, even when a lexical $_ is in scope
UNITCHECK
Blocks run just after the compilation of a file
package Foo;
UNITCHECK { warn "Foo was just loaded" }
UNITCHECK
Blocks run just after the compilation of a file
package Foo;
UNITCHECK { warn "Foo was just loaded" }
More useful than
CHECK blocks (which are run just after the compilation of the
main program).
No more pseudo-hashes
my $pseudohash = [
{ a => 1, b=> 2 },s
"foo",
"bar",
];
print $pseudohash->{a};
Gives "foo" with 5.8.x.
No more pseudo-hashes
my $pseudohash = [
{ a => 1, b=> 2 },s
"foo",
"bar",
];
print $pseudohash->{a};
Gives an error with 5.10.
$* and $# were removed
Deprecated for a long time
Were standing in the way of bug fixes
Lexical pragmas
It's now possible to write one's own lexical pragmas.
Lexical pragmas
It's now possible to write one's own lexical pragmas.
Example:
encoding::source on CPAN.
Lexical pragmas
It's now possible to write one's own lexical pragmas.
Example:
encoding::source on CPAN.
Other pragmas are now completely lexical.
(
sort,
bignum,
bigint,
bigrat,
less)
Less side effects!
Regular expressions
- Recursion
- Named captures
- Relative backreferences
- Possessive quantifiers (?+, *+, ++)
- Backtracking control verbs
- \K
Recursion
How to match balanced parenthesis pairs:
/ ^
( \(
(?:
(?> [^()]+ ) | (?1)
)*
\) )
$ /x
Named captures
/(?<char>.)\k<char>/
and say "'$+{char}' is the first doubled character";
Relative backreferences
# Matches 'abab', 'cdcd', etc.
my $qr = qr /(.)(.)\g{-2}\g{-1}/;
# Matches 'ababcdcd'
/$qr$qr/
Possessive quantifiers
?+
*+
++
Like ?, *, +, but without any backtracking
Backtracking control verbs
(*ACCEPT) end matching with success
(*COMMIT) fail if backtracked into
(*FAIL) end matching with failure
(*MARK) mark a matching point
(*SKIP) skip to here or to a mark
(*PRUNE) don't backtrack past this point
(*THEN) (*PRUNE) and try next alternation
\K
An assertion that keeps whatever was matched until now, and not including it in
$&.
s/(foo)bar/$1/g;
===
s/foo\Kbar//g;
=>
variable-length lookbehind
Field hashes
- New module Hash::Util::FieldHash
Field hashes
- New module Hash::Util::FieldHash
- Associates references to values
Field hashes
- New module Hash::Util::FieldHash
- Associates references to values
- Can be used to implement efficiently inside-out objects
Optimisations
- The internal data structures take less space
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
- Several UTF-8 operations are faster
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
- Several UTF-8 operations are faster
- Access through a constant index to lexical arrays is faster
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
- Several UTF-8 operations are faster
- Access through a constant index to lexical arrays is faster
- Constants take less memory
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
- Several UTF-8 operations are faster
- Access through a constant index to lexical arrays is faster
- Constants take less memory
- The creation of anonymous hashes and arrays is faster
Optimisations
- The internal data structures take less space
- Several speed optimisations on the regex engine
- Several UTF-8 operations are faster
- Access through a constant index to lexical arrays is faster
- Constants take less memory
- The creation of anonymous hashes and arrays is faster
- ...
Better error messages
With 5.8.8:
$ perl -we 'my $foo; print $foo'
Use of uninitialized value in print at -e line 1.
Better error messages
With 5.10:
$ perl -we 'my $foo; print $foo'
Use of uninitialized value $foo in print at -e line 1.
Conclusion
- Better adapted to big software projects
Conclusion
- Better adapted to big software projects
- Better adapted to performance-demanding applications
Conclusion
- Better adapted to big software projects
- Better adapted to performance-demanding applications
- Just better!
Conclusion
- Better adapted to big software projects
- Better adapted to performance-demanding applications
- Just better!
- Any questions?