what's new in perl 5.10?

Download What's new in Perl 5.10?

If you can't read please download the document

Upload: acme

Post on 16-Apr-2017

34.763 views

Category:

Business


3 download

TRANSCRIPT

What is new in Perl 5.10?

Paul Fenwick

Audrey Tang

Leon Brocard

slidehist

Monday: I'm presenting?

Tuesday: download Paul's slides

Wednesday: tweak design

Thursday: play with fonts

Friday: argh slides on a plane

Saturday: release!

perlhist

1987: 1.0

1988: 2.0

1989: 3.0...

1991: 4.0...

1994: 5.0

perlhist 5.0

1994: 5.0

1995: 5.1

1996: 5.2, 5.3

1997: 5.4

1998: 5.5...

2000: 5.6...

2002: 5.8

5.8 perlhist

2002: 5.8.0

2003: 5.8.1, 5.8.2

2o04: 5.8.3, 5.8.4, 5.8.5, 5.8.6

2005: 5.8.7

2006: 5.8.8

2007

Stable: 5.8.9

New: 5.10

New new: 6.0.0

New features

Perl 5.10 has new features

Sorry Sebastian

New?

new lexically-scoped feature pragma:

use feature qw(say switch);

use feature qw(:5.10);

use 5.10;

Optimise programmer time

Most advanced programming language ever!

Perl: print hello\n

Perl 6: say hello

Perl 5.10: say hello

Save typing

My poor fingers!

Also none of this nasty quoting business

This might seem like it's trivial...

Or

$c = $a | | $b is handy

confused by empty string, undef or zero

Defined or

$c = $a // $b is handier

same as $c = defined($a) ? $a : $b

dor and err

There's also low-precedence //

use feature qw(dor err);

fileno($x) dor die 'That's not a filehandle';

fileno($x) err die 'That's not a filehandle';

State variables

The old way of having a persistent variable:

{
my $i = 0;
sub incrementor {
return $i++;
}
}

State variables

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

Photographer: Jason ZackLicense: CC-by-SA

http://en.wikipedia.org/wiki/Image:On-Off_Switch.jpg

Guessing Game

use feature qw(switch say);
my @guessed;
my $num = int(rand 100)+1;
while (my $guess = ) {
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');

Photographer: Graham BirdLicense: Free Use

http://commons.wikimedia.org/wiki/Image:Pitts-S1S-in-flight.jpg

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]/

Aho-Corasick start-point optimisation

Regex engine

Pluggable: use re::engine::PCRE;

Named capture buffers

/$(?\d+)/ ... $+{price}

More

Other goodies

Misc Attribute Decoration

$AUTOLOAD can be tainted

Source filters in @INC

encodings::warnings is lexical

Better threads

Faster UTF-8

More other goodies

Faster stat() on Windows

Relocatable installs

Overloading for re-blessed objects

Better Windows support

Smaller memory footprint (slightly faster)

More documentation

More details?

http://search.cpan.org/dist/perl/pod/perl594delta.pod

Photographer:NASALicense: Public Domain

http://commons.wikimedia.org/wiki/Image:Hubble_01.jpg

Questions?

When will it be out?

Stable: 5.8.9 by April 1st

New: 5.10 after lunch, before Christmas

New new: 6.0.0 after lunch, before Christmas

Any other questions?

License

These slides are Copyright 2006 Paul Fenwick, Audrey Tang, Leon Brocard

The text may be distributed under your choice of any of the following:

The license terms of Perl itself

GNU Free Documentation License

Creative Commons Attribution ShareAlike

Click to edit the title text format

Click to edit the outline text format

Second Outline Level

Third Outline Level

Fourth Outline Level

Fifth Outline Level

Sixth Outline Level

Seventh Outline Level

Eighth Outline Level

Ninth Outline Level