Whatsnew in-perl

146
Perl’s Modern Features

Transcript of Whatsnew in-perl

Page 1: Whatsnew in-perl

Perl’s Modern Features

Page 2: Whatsnew in-perl

Dave Oswald

● Conferences Committee Chairman

● Software Engineer

● Platform & Infrastructure Engineering Manager

Page 3: Whatsnew in-perl
Page 4: Whatsnew in-perl
Page 5: Whatsnew in-perl

A brief history of time...

Perl 1: December 1987Perl 2: June 1988Perl 3: October 1989Perl 4: March 1991Perl 5: October 1994

Page 6: Whatsnew in-perl

Perl 5 vs Perl 6

Page 7: Whatsnew in-perl

Perl 5 and Perl 6 are different languages with similar design philosophies.

Page 8: Whatsnew in-perl

Perl 5 development and innovation will continue as Perl 5.x for the foreseeable future.

Page 9: Whatsnew in-perl

Perl 5 Versions

● Ancient History– 5.001 (1995)

– 5.002 (1996)

– 5.003 (1996)

– 5.004 (1997)

– 5.005 (1998)

● Middle History– 5.6 (2000)

– 5.8 (2002)

Page 10: Whatsnew in-perl

Stagnation in the Perl 5.8 era

Page 11: Whatsnew in-perl

Perl 5 Versions

● Modern Perl– 5.10 (2007)

● 2008● 2009

– 5.12 (2010)

– 5.14 (2011)

– 5.16 (2012)

– 5.18 (2013)

● Middle (Dark) Ages– 5.6 (2000)

– 5.8 (2002)● 2003

● 2004

● 2005

● 2006

● 2007

Page 12: Whatsnew in-perl

Annual Release Cycles Since 2010(Perl 5.12)

Page 13: Whatsnew in-perl

Perl 5.26(2017)

Page 14: Whatsnew in-perl

Filling in of the core ecosystem

Page 15: Whatsnew in-perl

Core comparison

● Perl 5.000

– Core: 1MB (53 files)

– Libs: 250KB (76 files)

– Exts: 216KB (38 files)

– Tests: 154KB (92 files)

– Docs: 536KB (62 files)

– Total: 2.2MB (321 files)

● Perl 5.26.0

– Core: 9MB (121 files)

– Libs: 24MB (1200 files)

– Exts: 40MB (3017 files)

– Tests: 10MB (2614 files)

– Docs: 8MB (211 files)

– Total: 91MB (7163 files)

Page 16: Whatsnew in-perl

corelist

Page 17: Whatsnew in-perl
Page 18: Whatsnew in-perl
Page 19: Whatsnew in-perl
Page 20: Whatsnew in-perl

Perl 5.6.x (2000)

Page 21: Whatsnew in-perl

open FH, ‘>foo.txt’;

Page 22: Whatsnew in-perl

open my $fh, ‘>’, ‘foo.txt’

Page 23: Whatsnew in-perl

{ open my $fh, ‘<’, ‘foo.txt’; my @input = <$fh>;}

# $fh falls out of scope, filehandle closed.

Lexical Filehandles

Page 24: Whatsnew in-perl

Three arg open

● Two-argument open exposes the shell.

open my $fh, “>$foo”

$foo = “/tmp/foo; rm -rf /”

● Three-argument open does not expose the shell.

open my $fh, ‘>’, $foo

Page 25: Whatsnew in-perl

5.8.x (2002-2007)

Page 26: Whatsnew in-perl

PerlIO

open my $fh, ‘>:utf8’, $file;

open my $fh, ‘<+’, \$memory;

Page 27: Whatsnew in-perl

5.8.7(The minimum version anyone should ever use)

sitecustomize.pl

Page 28: Whatsnew in-perl

Set up a sitecustomize.pl for all of your perlbrew, plenv, and

containers.

Page 29: Whatsnew in-perl
Page 30: Whatsnew in-perl

6 years

Page 31: Whatsnew in-perl

5.10 (2007)

Page 32: Whatsnew in-perl

The Renaissance of Perl

Page 33: Whatsnew in-perl

This is the stuff you might have missed

Page 34: Whatsnew in-perl

New Operators:

Defined

Page 35: Whatsnew in-perl

$foo = $bar if ! $foo;

Page 36: Whatsnew in-perl

$foo ||= $bar

$foo = $bar if ! $foo;

Page 37: Whatsnew in-perl

$foo = $bar if ! defined $foo;

Page 38: Whatsnew in-perl

$foo //= $bar;

$foo = $bar if ! defined $foo;

Page 39: Whatsnew in-perl

$foo = ($bar) ? $bar : $baz;

Page 40: Whatsnew in-perl

$foo = $bar || $baz;

$foo = ($bar) ? $bar : $baz;

Page 41: Whatsnew in-perl

$foo = (defined $bar) ? $bar : $baz;

Page 42: Whatsnew in-perl

$foo = $bar // $baz;

$foo = (defined $bar) ? $bar : $baz;

Page 43: Whatsnew in-perl

Smartmatch

~~

Page 44: Whatsnew in-perl

(Too) Smartmatch

~~

Page 45: Whatsnew in-perl

print “Yes!” if 42 ~~ @array;

print “Yes” if grep {m/^42$/} @array

Page 46: Whatsnew in-perl

Nearly all Perl operators are monomorphic, while data-types are polymorphic.

Smartmatch is polymorphic.

Combining polymorphic operators with polymorphic data is madness.

Page 47: Whatsnew in-perl
Page 48: Whatsnew in-perl

23 Rules

Page 49: Whatsnew in-perl

Smartmatch is deprecated in its current form.

Page 50: Whatsnew in-perl

given / when

Page 51: Whatsnew in-perl

given ($foo) {when (/^abc/) { $abc = 1; }when (/^def/) { $def = 1; }when (/^xyz/) { $xyz = 1; }default { $nothing = 1; }

}

Page 52: Whatsnew in-perl

Perl’s version of a switch statement

Page 53: Whatsnew in-perl

given / when uses smartmatch semantics

Page 54: Whatsnew in-perl

We just said that smartmatch is madness.

...and that it’s deprecated.

Page 55: Whatsnew in-perl

Perl doesn’t really need a switch statement

Page 56: Whatsnew in-perl

given / when are deprecated in their current form

Page 57: Whatsnew in-perl

say

Page 58: Whatsnew in-perl

print “Hello world\n”;

Page 59: Whatsnew in-perl

say “Hello world”;

Page 60: Whatsnew in-perl

use feature ‘say’;

say “Hello world”;

Page 61: Whatsnew in-perl

perl -e ‘print “Hello world\n”’

perl -E ‘say “Hello world”;’

Page 62: Whatsnew in-perl

use feature qw(say state);

feature Pragma

Page 63: Whatsnew in-perl

feature Pragma

● say● state● switch● unicode_strings● unicode_eval● current_sub● array_base

● lexical_subs● postderef● postderef_qq● signatures● refaliasing● bitwise● declared_refs

Page 64: Whatsnew in-perl

state

Page 65: Whatsnew in-perl

state

{

my $foo = 1;

sub count {

return $foo++;

}

}

say count(); # 1

say count(); # 2

say count(); # 3

sub count {

state $foo = 1;

return $foo++;

}

say count(); # 1

say count(); # 2

say count(); # 3

Page 66: Whatsnew in-perl

Named Capture Buffers

Page 67: Whatsnew in-perl

$string =~ m/a(b)c/;

say $1; # “b” if $string matched.

Page 68: Whatsnew in-perl

$string =~ m/a(?<foo>b)c/;

say $+{foo}; # “b” if $string matched.

Page 69: Whatsnew in-perl

Perl 5.12.x (2010)

Page 70: Whatsnew in-perl

delete local

Page 71: Whatsnew in-perl

our $foo = 10;{ local $foo = 20; say $foo; # 20}say $foo; # 10

Page 72: Whatsnew in-perl

my %hash = (foo => 1, bar => 2);

{ local $hash{foo} = 4; say $hash{foo}; # 4}

say $hash{foo}; # 1

Page 73: Whatsnew in-perl

my %hash = (foo => 1, bar => 2);

{ my $bar = delete local $hash{foo}; say $bar; # 1; say $hash{foo}; # undef}

say $hash{foo}; # 1

Page 74: Whatsnew in-perl

package NAME VERSION

● package Foo::Bar;

our $VERSION = 1.23;

● package Foo::Bar 1.23;

Page 75: Whatsnew in-perl

Perl 5.14 (2011)

Page 76: Whatsnew in-perl

/d, /l, /u, /a regexp flags

● Modifiers to alter regex Unicode vs ASCII semantics.

– enough said.

Page 77: Whatsnew in-perl

Perl 5.16.x (2012)

Page 78: Whatsnew in-perl

__SUB__

Page 79: Whatsnew in-perl

sub factorial { my ($x) = @_; return 1 if $x == 1; return($x * factorial($x - 1));};

say factorial(5);

Page 80: Whatsnew in-perl

my $factorial = sub { my ($x) = @_; return 1 if $x == 1; return($x * __SUB__->($x - 1));};

say $factorial->(5);

Page 81: Whatsnew in-perl

Perl 5.18.x (2013)

Page 82: Whatsnew in-perl

Extended Bracketed Character Classes

(Set operations on character classes)

Page 83: Whatsnew in-perl

/(?[ \p{Thai} & \p{Digit} ])/

Page 84: Whatsnew in-perl

Lexical Subroutines

Page 85: Whatsnew in-perl

sub foo { say “Goodbye world!” }

{ my sub foo { say “Hello world!” } foo(); # Hello world!}

foo(); # Goodbye world!

Page 86: Whatsnew in-perl

perl 5.20.x (2014)

Page 87: Whatsnew in-perl

**** Subroutine Signatures ****

Page 88: Whatsnew in-perl

sub add { die "Too many arguments for subroutine" unless @_ <= 2; die "Too few arguments for subroutine" unless @_ >= 2;

my ($x, $y) = @_;

return $x + $y;}

Page 89: Whatsnew in-perl

sub add ($x, $y) { return $x + $y;}

Page 90: Whatsnew in-perl

sub greet ($name = ‘World’) { say “Hello $name!”;}

hello(); # Hello World!

hello(‘Dave’); # Hello Dave!

Page 91: Whatsnew in-perl

sub greet { die "Too many arguments for subroutine" unless @_ <= 1; my $name = $_[0] // ‘World’;

say “Hello $name!”;}

greet(); # Hello World!

greet(‘Dave’); # Hello Dave!

Page 92: Whatsnew in-perl

postfix dereferencing

Page 93: Whatsnew in-perl
Page 94: Whatsnew in-perl

my @values = @{$args->{foo}->[0]}

Page 95: Whatsnew in-perl

my @values = @{$args->{foo}->[0]}

my @values = $args->{foo}->[0]->@*

Page 96: Whatsnew in-perl

my @values = $args->{foo}->[0]->@*

my @values = $args->{foo}[0]->@*

Page 97: Whatsnew in-perl

/r

(Non-destructive substitution)

Page 98: Whatsnew in-perl

my $string = “Hello world!”;my $copy = $string;$copy =~ s/world/David/;

Page 99: Whatsnew in-perl

my $string = “Hello world!”;

(my $copy = $string) =~ s/world/David/;

Page 100: Whatsnew in-perl

my $string = “Hello world!”;

my $copy = $string =~ s/world/David/r;

Page 101: Whatsnew in-perl

my $translaterated = $string =~ tr/a-f/A-F/r

Page 102: Whatsnew in-perl

Perl 5.22 (2015)

Page 103: Whatsnew in-perl

Double Diamond Operator

Page 104: Whatsnew in-perl

while(<>) { chomp; say $_ * 2;}

Page 105: Whatsnew in-perl

while(<<>>) { chomp; say $_ * 2;}

Page 106: Whatsnew in-perl

Why?

Page 107: Whatsnew in-perl

./myscript ‘|rm -rf’

Diamond operator implicitly opens files named in @ARGV.

<> is like 2-arg open: Exposes the shell.

<<>> is like 3-arg open. Doesn’t expose the shell.

Page 108: Whatsnew in-perl

perl 5.24.x (2016)

Page 109: Whatsnew in-perl

BORING

Page 110: Whatsnew in-perl

perl 5.26.x (2017)

Page 111: Whatsnew in-perl

Indented HERE docs

Page 112: Whatsnew in-perl

sub foo { print <<’HERE’;Hello world.This is pretty ugly code formatting.HERE}

Page 113: Whatsnew in-perl

sub foo { print <<~’HERE’; Hello world. This is much nicer formatting. HERE}

Page 114: Whatsnew in-perl

/xx

Page 115: Whatsnew in-perl

m/([+-])([\da-f]+)/

Page 116: Whatsnew in-perl

m/ ([+-]) ([\da-f]+)/x

Page 117: Whatsnew in-perl

m/ ([+-]) ([ \d a-f ]+)/xx

Page 118: Whatsnew in-perl

. removed from @INC

Page 119: Whatsnew in-perl

.

cwd

Page 120: Whatsnew in-perl

@INC

Page 121: Whatsnew in-perl

What’s the problem?

● Different versions of a module can load based on where a script is run from.

● The script’s behavior changes based on where we run it from.– From ~/ it may skip an optional dependency.

– From ~/scripts may find an optional dependency in @INC and load it.

● User-writable directories such as /tmp were unsafe as a working directory for running any script.

Page 122: Whatsnew in-perl

As long as . is in @INC you cannot ever be sure when you run a script from a path that is writable

by anyone aside from yourself.

Page 123: Whatsnew in-perl

You cannot ever be sure modules you are using don’t have optional dependencies.

Page 124: Whatsnew in-perl

Steps to protect yourself

● Use Perl 5.26

Page 125: Whatsnew in-perl

Perl 5.26 is the minimum Perl version anyone should ever use

Page 126: Whatsnew in-perl

Impractical advice.

Page 127: Whatsnew in-perl

Steps to protect yourself

● Use Perl 5.26● Set up a sitecustomize.pl

– System, perlbrew, plenv, containers

Page 128: Whatsnew in-perl

Steps to protect yourself

● Use Perl 5.26● Set up a sitecustomize.pl

– System, perlbrew, plenv, containers

– Perl must be built with -Dusesitecustomize

– Check state usingperl -MConfig -E ‘say $Config{usesitecustomize}’

Page 129: Whatsnew in-perl

Set up sitecustomize.pl if your distribution doesn’t have one

already.

Page 130: Whatsnew in-perl

That’s all that has happened since Perl 5.6 in 2000?

Page 131: Whatsnew in-perl

Hundreds... thousands of bug fixes, performance improvements, documentation enhancements,

and other features we don’t have time to discuss.

Page 132: Whatsnew in-perl

Thousands of...

● Security Patches / Enhancements● Bug fixes● Performance Enhancements● Documentation Enhancements● New features● Unicode improvements● Etc.

Page 133: Whatsnew in-perl

Examples

● Improved integer performance● Better hash key order randomization to inhibit

DOS attacks.● Many Unicode upgrades and improvements● fc (fold case)

● More module improvements than you can shake a stick at.

● Thousands of pages of added documentation.

Page 134: Whatsnew in-perl

How do I know when feature X was added to Perl?

Page 135: Whatsnew in-perl

How do I know when feature X was added to Perl?

Page 136: Whatsnew in-perl

How do I know when feature X was added to Perl?

grep -rin 'signatures' $(find $(perl -E 'say for @INC') -name '*delta.pod')

Page 137: Whatsnew in-perl

Encourage upgrades to modern Perl versions.

Page 138: Whatsnew in-perl

Rather than upgrading “system Perl” use perlbrew or plenv.

Page 139: Whatsnew in-perl
Page 140: Whatsnew in-perl

Object Orientation

package Foo

sub new {

my $class = shift;

return bless {@_}, $class;

}

sub name {

return $_[0]->{‘name’};

}

use Moo;

has name => (is => ro);

Page 141: Whatsnew in-perl

Many wonderful Modules

● Try::Tiny

● Capture::Tiny

● List::MoreUtils

● Mojolicious

● Moose or Moo

● CPAN has over 20000 distributions.

Page 142: Whatsnew in-perl

A concept is only valid in Perl if it can be expressed as a one-liner

$ perl -Mojo -E 'say g("mojolicious.org")->dom("h1")->map("text")->join("\n")'

Page 143: Whatsnew in-perl
Page 144: Whatsnew in-perl

Perl development is alive and well.

(Rumors to the contrary are grossly exaggerated)

Page 145: Whatsnew in-perl

Modern Perl

modernperlbooks.com

Page 146: Whatsnew in-perl

perldoc perldelta

perldoc perlhist

corelist