use feature 'state'; sub incrementor { state $i = 0; return $i++; }
Local caches
State variables can live deep inside subroutines...
for my $x (...) { for my $y (...) { state %seen; next if $seen{$x}{$y}++; } }
Switch
Perl 5.10 introduces a native switch statement.
Similar to Perl 6's switch, and Switch.pm (source filter)
Built into the Perl interpreter
Guessing Game
use feature qw(switch say); my @guessed; my $num = int(rand 100)+1; while (my $guess = <STDIN>) { chomp $guess; given($guess) { when (/D/) { say "Give me an integer" } when (@guessed) { say "You've tried that" } when ($num) { say "Just right!"; last } when ($_ < $num) { say "Too low"; continue } when ($_ > $num) { say "Too high"; continue } push(@guessed,$_); } }
foreach / when
use feature 'switch'; foreach (@cool_things) { when (/pirate/) { $pirate++ } when (/ninja/) { $ninja++ } when (/robot/) { $robot++ } say "$_ doesn't look cool..."; }
when automatically calls next at the end of its block
Smart-match
use feature '~~'; if ($x ~~ @array) { say "$x exists" } if ($x ~~ /ninja/) { say "Ninja in string"} if (@x ~~ /ninja/) { say "Ninja in array" } if ($key ~~ %hash) { say "$key exists" } if ($subref ~~ $arg) { say 'sub($arg) true' }
Smart-match
use feature '~~'; if (@array ~~ $x) { say "$x exists" } if (/ninja/ ~~ $x) { say "Ninja in string"} if (/ninja/ ~~ @x) { say "Ninja in array" } if (%hash ~~ $key) { say "$key exists" } if ($arg ~~ $subref) { say 'sub($arg) true' }
~~ can be overloaded
UNIVERSAL::DOES
Traditionally isa would be used to determine the capabilities of a class:
provides a way to show compatibility, without inheritance
$obj‐>isa('Logger');
$obj‐>DOES('Logger');
Helicopter
package Helicopter; sub DOES { my ($this, $interface) = @_; return 1 if ($interface eq "Airplane"); return $this->SUPER::does($interface); }
Constant (un-)folding
Perl 'folds' constants at compile time
9+5 gets converted to 14
In Perl 5.10, exceptional constants are unfolded
They'll still throw run-time exceptions if you try to execute them
Hash::Util::FieldHash
5.10 comes with support for Field Hashes, which:
Can use references as hash keys
Reference-keys migrate correctly across threads
Entries to stale references are automatically deleted
(Inside-Out objects work nicely)
Assertions
Perl 5.10 implements real assertions
Normally compiled away
Can be enabled on a per-module basis
# This is normally compiled away sub assert_sanity :assertion {...} # Enable assertions for MyModule perl -A=MyModule prog.pl
User-defined lexical pragmata
We've used lexical pragmata for years: { use strict; ... }
In Perl 5.10 you can write your own!
%^H allows "hints" to be attached to the optree
use feature is implemented this way
More modules
encoding::warnings
Math::BigInt::FastCalc
Time::Piece
Win32API::File
CPANPLUS
CPANPLUS
Hi Jos!
It's about time, isn't it?
Means we have many other modules in the core...
Regex engine
perlreguts
No longer recursive
Single char char-classes treated as literals
Trie optimisation of literal string alternations /foam|foal|foad/ => /foa[mld]/
1–1 of 1 previous next