+ All Categories
Home > Business > Perl 5.10 for People Who Aren't Totally Insane

Perl 5.10 for People Who Aren't Totally Insane

Date post: 11-Jun-2015
Category:
Upload: ricardo-signes
View: 84,309 times
Download: 1 times
Share this document with a friend
Description:
All the hype about perl 5.10 can sound a little intimidating. User-level pragmata! Overloadable smartmatching operator! Thread-safe refkey hashes! For Pete's sake, have you heard about lexically scoped pluggable regexp engines? It's enough to make you think that 5.10's changes are just for the hard-core perl hackers, but it couldn't be further from the truth! The new version of Perl is full of changes that are easy to use and pack lots of useful benefits for doing plain old every day Perl programming. We'll look at the new features, small and large, and see why you, too, will love 5.10.
Popular Tags:
184
Perl 5.10 insane for people who are not
Transcript
Page 1: Perl 5.10 for People Who Aren't Totally Insane

Perl 5.10insanefor people who are not

Page 2: Perl 5.10 for People Who Aren't Totally Insane

Perl 5.10insanetotallyfor people who are not

Page 3: Perl 5.10 for People Who Aren't Totally Insane

perl5100delta

5.10 isn’t like 5.8.x

Page 4: Perl 5.10 for People Who Aren't Totally Insane

perl5100delta

5.10 isn’t like 5.8.x

- features only get added in new 5.x releases

Page 5: Perl 5.10 for People Who Aren't Totally Insane

perl5100delta

5.10 isn’t like 5.8.x

- features only get added in new 5.x releases

- it’s been 5 years since the last release (5.8)

Page 6: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

5.10 is Way Cool

Page 7: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

5.10 is Way Cool

- no significant new features in Perl since 2002

Page 8: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

5.10 is Way Cool

- no significant new features in Perl since 2002

- remember how bad you wanted to see Star Wars: Episode 1?

Page 9: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

5.10 is Way Cool

- no significant new features in Perl since 2002

- remember how bad you wanted to see Star Wars: Episode 1?

- that’s how excited you should be for 5.10

Page 10: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

5.10 is Way Cool

- no significant new features in Perl since 2002

- remember how bad you wanted to see Star Wars: Episode 1?

- that’s how excited you should be for 5.10

- but it won’t suck (no POD race scene)

Page 11: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Lexicascopasmartwhat?

Page 12: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Lexicascopasmartwhat?

- lexically scoped user pragmata!

Page 13: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Lexicascopasmartwhat?

- lexically scoped user pragmata!

- pluggable regex compilation engines!

Page 14: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Lexicascopasmartwhat?

- lexically scoped user pragmata!

- pluggable regex compilation engines!

- trie-based non-recursive pattern matching!

Page 15: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Lexicascopasmartwhat?

- lexically scoped user pragmata!

- pluggable regex compilation engines!

- trie-based non-recursive pattern matching!

- thread-safe weak refkey hashes!

Page 16: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Yes, You Care

Page 17: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Yes, You Care

- Not everything in 5.10 is esoteric.

Page 18: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Yes, You Care

- Not everything in 5.10 is esoteric.

- Not everything in 5.10 is for gurus.

Page 19: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Yes, You Care

- Not everything in 5.10 is esoteric.

- Not everything in 5.10 is for gurus.

- Not everything in 5.10 is for C programmers.

Page 20: Perl 5.10 for People Who Aren't Totally Insane

perl51000delta

Yes, You Care

- Not everything in 5.10 is esoteric.

- Not everything in 5.10 is for gurus.

- Not everything in 5.10 is for C programmers.

- Not everything in 5.10 is super advanced.

Page 21: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

Page 22: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

Page 23: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

Page 24: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

Page 25: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

- use feature ‘mtfnpy’;

Page 26: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

- use feature ‘mtfnpy’;

- use 5.010;

Page 27: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

- use feature ‘mtfnpy’;

- use 5.010;

- read the perldoc

Page 28: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

- use feature ‘mtfnpy’;

- use 5.010;

- read the perldoc I’m a perldoc ref!

Page 29: Perl 5.10 for People Who Aren't Totally Insane

feature

First: A Warning

- 5.10 is backwards compatible

- but adds new keywords and operators

- they’re not enabled by default

- use feature ‘mtfnpy’;

- use 5.010;

- read the perldoc

Page 30: Perl 5.10 for People Who Aren't Totally Insane

say what

Page 31: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

- new built-in, say

- it’s like print

- but it adds a newline for you

say $what

Page 32: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

Page 33: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

Page 34: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

print “$message\n”;

Page 35: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

print “$message\n”;

print “$_\n” for @lines;

Page 36: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

print “$message\n”;

print “$_\n” for @lines;

say “Hello, world!”;

Page 37: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

print “$message\n”;

print “$_\n” for @lines;

say “Hello, world!”;

say $message;

Page 38: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

say $what

print “Hello, world!\n”;

print “$message\n”;

print “$_\n” for @lines;

say “Hello, world!”;

say $message;

say for @lines;

Page 39: Perl 5.10 for People Who Aren't Totally Insane

truth and definedness

Page 40: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

Page 41: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale {

Page 42: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

Page 43: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

$amount ||= $product->cost;

Page 44: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

$amount ||= $product->cost;

...

Page 45: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

$amount ||= $product->cost;

...}

Page 46: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definednesssub record_sale { my ($product, $amount) = @_;

$amount = defined $amount ? $amount : $product->cost;

...}

Page 47: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

$amount ||= $product->cost;

...}

Page 48: Perl 5.10 for People Who Aren't Totally Insane

perlop

truth and definedness

sub record_sale { my ($product, $amount) = @_;

$amount ||= $product->cost;

...}

Page 49: Perl 5.10 for People Who Aren't Totally Insane

perlop

the new OR operator

sub record_sale { my ($product, $amount) = @_;

$amount //= $product->cost;

...}

Page 50: Perl 5.10 for People Who Aren't Totally Insane

perlop

the new OR operator

$setting = defined $given ? $given : $default;

Page 51: Perl 5.10 for People Who Aren't Totally Insane

perlop

the new OR operator

$setting = $given;unless (defined $setting) { $setting = $default;}

Page 52: Perl 5.10 for People Who Aren't Totally Insane

perlop

the new OR operator

$setting = $given || $default;

Page 53: Perl 5.10 for People Who Aren't Totally Insane

perlop

the new OR operator

$setting = $given // $default;

Page 54: Perl 5.10 for People Who Aren't Totally Insane

keeping state

Page 55: Perl 5.10 for People Who Aren't Totally Insane

perlsub

$lines_left = 100;

sub read_line { die “trial period expired” unless $lines_left-- > 0; ...}

State Variables

Page 56: Perl 5.10 for People Who Aren't Totally Insane

perlsub

my $lines_left = 100;

sub read_line { die “trial period expired” unless $lines_left-- > 0; ...}

State Variables

Page 57: Perl 5.10 for People Who Aren't Totally Insane

perlsub

{ my $lines_left = 100;

sub read_line { die “trial period expired” unless $lines_left-- > 0; ... }}

State Variables

Page 58: Perl 5.10 for People Who Aren't Totally Insane

perlsub

package Trial::Period;sub new { my ($class, $arg) = @_; my $guts = { lines_left => $arg->{lines}, error_msg => $arg->{error}, }; return bless $guts => $class;}

sub consume_line { my ($self) = @_; $self->{lines_left}--;}

sub lines_left { my ($self) = @_; return $self->{lines_left};}

sub assert_lines_left { my ($self) = @_; unless ($self->lines_left) { die $self->{error_msg}; }}1;

State Variables

my $LINES = 100;my $ERROR = “sorry, trial period over”;my $TRIAL = Trial::Period->new({ lines => $LINES, error => $ERROR,});

sub read_line { $TRIAL->assert_lines_left; ...}

Page 59: Perl 5.10 for People Who Aren't Totally Insane

perlsub

{ my $lines_left = 100;

sub read_line { die “trial period expired” unless $lines_left-- > 0; ... }}

State Variables

Page 60: Perl 5.10 for People Who Aren't Totally Insane

perlsub

sub read_line { state $lines_left = 100; die “trial period expired” unless $lines_left-- > 0; ...}

State Variables

Page 61: Perl 5.10 for People Who Aren't Totally Insane

-x stacking

Page 62: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

if ( -f $fileand -w $fileand -z $file

) {unlink $file;

}

Stackable File Tests

Page 63: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

if ( -f $file and -w _ and -z _) {unlink $file;

}

Stackable File Tests

Page 64: Perl 5.10 for People Who Aren't Totally Insane

perlfunc

if (-f -w -z $file) {unlink $file;

}

Stackable File Tests

Page 65: Perl 5.10 for People Who Aren't Totally Insane

smart matching

Page 66: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

Page 67: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

- a new kind of comparison operator

Page 68: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

- a new kind of comparison operator

- its behavior depends on its inputs

Page 69: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

- a new kind of comparison operator

- its behavior depends on its inputs

- “if these two things match...”

Page 70: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

- a new kind of comparison operator

- its behavior depends on its inputs

- “if these two things match...”

- hard to tell, easy to show...

Page 71: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

Page 72: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }

Page 73: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }

Page 74: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }

Page 75: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }

Page 76: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }

Page 77: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }elsif ($foo ~~ $bar) { ... }

Page 78: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }elsif ($foo ~~ $bar) { ... }else { ... }

Page 79: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchinggiven ($foo) { when (undef) { ... } when (@array) { ... } when ($code) { ... } when (%hash) { ... } when (qr/re/) { ... } when ($bar) { ... } default { ... }}

Page 80: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ @array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ %hash) { ... }elsif ($foo ~~ qr/re/) { ... }elsif ($foo ~~ $bar) { ... }else { ... }

Page 81: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

if ($foo ~~ undef) { ... }elsif ($foo ~~ $array) { ... }elsif ($foo ~~ $code) { ... }elsif ($foo ~~ $hash) { ... }elsif ($foo ~~ qr/re/) { ... }elsif ($foo ~~ $bar) { ... }else { ... }

Page 82: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

my $test;

Page 83: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchinggiven ($foo) { when (undef) { ... } when ($aref) { ... } when ($code) { ... } when ($href) { ... } when ($regex) { ... } when ($object) { ... } default { ... }}

Page 84: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchinggiven ($foo) { when ($test_1) { ... } when ($test_2) { ... } when ($test_3) { ... } when ($test_4) { ... } when ($test_5) { ... } when ($test_6) { ... } default { ... }}

Page 85: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

Page 86: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where($test)

Page 87: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where($test)

@want = @have->where(sub{ …… })

Page 88: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where($test)

@want = @have->where(sub{ …… })

@want = @have->where(qr/.../sm)

Page 89: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where($test)

@want = @have->where(sub{ …… })

@want = @have->where(qr/.../sm)

@want = @have->where([ 1,2,3 ])

Page 90: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

Page 91: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where {

Page 92: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

Page 93: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) {

Page 94: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test;

Page 95: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array;

Page 96: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; }

Page 97: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) {

Page 98: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array;

Page 99: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; }

Page 100: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; } if (ref $test eq ‘CODE’) {

Page 101: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; } if (ref $test eq ‘CODE’) { return grep { $test->($_) } @$array;

Page 102: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; } if (ref $test eq ‘CODE’) { return grep { $test->($_) } @$array; }

Page 103: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; } if (ref $test eq ‘CODE’) { return grep { $test->($_) } @$array; }

die “invalid test”

Page 104: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matchingsub where { my ($array, $test) = @_;

if (ref $test eq ‘ARRAY’) { my %known = map {$_=>1} @$test; return grep { $known{$_} } @$array; } if (ref $test eq ‘Regexp’) { return grep { $_ =~ $test } @$array; } if (ref $test eq ‘CODE’) { return grep { $test->($_) } @$array; }

die “invalid test”}

Page 105: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

sub where { my ($array, $test) = @_; grep { $_ ~~ $test } @$array;}

Page 106: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

SmartMatch::Sugar

Page 107: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where( hash )

SmartMatch::Sugar

Page 108: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where( hash )

@want = @have->where( class )

SmartMatch::Sugar

Page 109: Perl 5.10 for People Who Aren't Totally Insane

perlsyn

Smart Matching

@want = @have->where( hash )

@want = @have->where( class )

@want = @have->where(isa(‘Foo’))

SmartMatch::Sugar

Page 110: Perl 5.10 for People Who Aren't Totally Insane

unknown undefined

Page 111: Perl 5.10 for People Who Aren't Totally Insane

perldiag

$str = “Greetings, $name. Your last login was $last. It is now $time.”;

Better Error Message(s)

Page 112: Perl 5.10 for People Who Aren't Totally Insane

perldiag

$str = “Greetings, $name. Your last login was $last. It is now $time.”;

Better Error Message(s)

Use of uninitialized value in concatenation (.) or string at hello.plx line 9.

Page 113: Perl 5.10 for People Who Aren't Totally Insane

perldiag

Better Error Message(s)

Use of uninitialized value $time in concatenation (.) or string at hello.plx line 9.

$str = “Greetings, $name. Your last login was $last. It is now $time.”;

Page 114: Perl 5.10 for People Who Aren't Totally Insane

inside-out objects

Page 115: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

Page 116: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

- traditional objects are a hashref

Page 117: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

- traditional objects are a hashref

- look up attrs in the obj itself by attr name

Page 118: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

- traditional objects are a hashref

- look up attrs in the obj itself by attr name

- inside-out objects are content-free refs

Page 119: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

- traditional objects are a hashref

- look up attrs in the obj itself by attr name

- inside-out objects are content-free refs

- look up attrs in an external hash by obj id

Page 120: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

- traditional objects are a hashref

- look up attrs in the obj itself by attr name

- inside-out objects are content-free refs

- look up attrs in an external hash by obj id

- there are complications for inside-out objects

Page 121: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objects

sub size { my $self = shift;

if (@_) { return $self->{size} = shift; } else { return $self->{size}; }}

Page 122: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objectsmy %size;sub size { my $self = shift;

if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; }}

Page 123: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objectsmy %size;sub size { my $self = shift; my $id = refaddr $self;

if (@_) { return $size{ $id } = shift; } else { return $size{ $id }; }}

Page 124: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objectsmy %size;sub size { my $self = shift; my $id = refaddr $self;

if (@_) { return $size{ $id } = shift; } else { return $size{ $id }; }}

sub DESTROY { my $id = refaddr $_[0]; delete $size{ $id };}

Page 125: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objectsmy %size;sub size { my $self = shift; my $id = refaddr $self;

if (@_) { return $size{ $id } = shift; } else { return $size{ $id }; }}

sub DESTROY { my $id = refaddr $_[0]; delete $size{ $id };}

sub CLONE { my $class = shift;

my @properties = map { values %$_ } values %PROP_DATA_FOR;

for my $old_id ( keys %OBJ ) {

my $object = $OBJ{ $old_id }; my $new_id = refaddr $object;

for my $prop ( @properties ) { next unless exists $prop->{ $old }; $prop->{ $new } = $prop->{ $old }; delete $prop->{ $old }; }

weaken ( $OBJ{ $new } = $object ); delete $OBJ{ $old }; }}

Page 126: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Inside-Out Objectsmy %OBJECT_REGISTRY;my %size;sub size { my $self = shift; my $id = refaddr $self;

$self->register_object;

if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; }}

sub DESTROY { my $id = refaddr $_[0]; delete $size{ $id }; delete $OBJECT_REGISTRY{ $id };}

sub register_object { my ($self) = @_; my $id = refaddr $self; $OBJECT_REGISTRY{ $id } = $self;}

sub CLONE { my $class = shift;

my @properties = map { values %$_ } values %PROP_DATA_FOR;

for my $old_id ( keys %OBJECT_REGISTRY ) {

my $object = $OBJECT_REGISTRY{ $old_id }; my $new_id = refaddr $object;

for my $prop ( @properties ) { next unless exists $prop->{ $old_id }; $prop->{ $new_id } = $prop->{ $old_id }; delete $prop->{ $old_id }; }

weaken ( $OBJECT_REGISTRY{ $new_id } = $object ); delete $OBJECT_REGISTRY{ $old_id }; }}

Page 127: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashes

Page 128: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashes

- they’re just like hashes

Page 129: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashes

- they’re just like hashes

- objects as keys become object ids

Page 130: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashes

- they’re just like hashes

- objects as keys become object ids

- but get destroyed and cloned

Page 131: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashesmy %size;sub size { my $self = shift;

if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; }}

Page 132: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashesfieldhash my %size;sub size { my $self = shift;

if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; }}

Page 133: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashes{ fieldhash my %size; sub size { my $self = shift; if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; } }}

Page 134: Perl 5.10 for People Who Aren't Totally Insane

Hash::Util::FieldHash

Field Hashessub size { my $self = shift; fieldhash state %size;

if (@_) { return $size{ $self } = shift; } else { return $size{ $self }; }}

Page 135: Perl 5.10 for People Who Aren't Totally Insane

lexical topic

Page 136: Perl 5.10 for People Who Aren't Totally Insane

(topic is how you say $_)

Page 137: Perl 5.10 for People Who Aren't Totally Insane

perlvar

for (@lines) { chomp; next if /^#/; next unless length; s/a/b/; sanity_check; say;}

Lexical Topic

Page 138: Perl 5.10 for People Who Aren't Totally Insane

perlvar

Lexical Topic

for my $line (@lines) { chomp $line; next if $line ~~ /^#/; next unless length $line; $line =~ s/a/b/; sanity_check($line); say $line;}

Page 139: Perl 5.10 for People Who Aren't Totally Insane

perlvar

for my $_ (@lines) { chomp; next if /^#/; next unless length; s/a/b/; sanity_check; say;}

Lexical Topic

Page 140: Perl 5.10 for People Who Aren't Totally Insane

Regex

Page 141: Perl 5.10 for People Who Aren't Totally Insane

named captures

Page 142: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Named Captures

Page 143: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Named Captures

- find matches by name, not position

Page 144: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Named Captures

- find matches by name, not position

- avoid the dreaded $1

Page 145: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Named Captures

- find matches by name, not position

- avoid the dreaded $1

- no longer second to Python or .Net!

Page 146: Perl 5.10 for People Who Aren't Totally Insane

perlre

# our hypothetical format

section:property = value

Regex: Named Captures

Page 147: Perl 5.10 for People Who Aren't Totally Insane

perlre

$line ~~ /(\w+):(\w+) = (\w+)/;

$name = $2;$value = $3;

Regex: Named Captures

Page 148: Perl 5.10 for People Who Aren't Totally Insane

perlre

$lhs_re = qr/(\w+):(\w+)/;$rhs_re = qr/(\w+)/;

$line ~~ /$lhs_re = $rhs_re/;

$name = $2;$value = $3;

Regex: Named Captures

Page 149: Perl 5.10 for People Who Aren't Totally Insane

perlre

$line ~~ /$lhs_re = $rhs_re/;

$name = $2;$value = $3;

Regex: Named Captures

Page 150: Perl 5.10 for People Who Aren't Totally Insane

perlre

$line ~~ /$lhs_re = $rhs_re/;

$name = $+{ name }; $value = $+{ value };

Regex: Named Captures

Page 151: Perl 5.10 for People Who Aren't Totally Insane

perlre

$lhs_re = qr/(\w+):(\w+)/;$rhs_re = qr/(\w+)/;

Regex: Named Captures

Page 152: Perl 5.10 for People Who Aren't Totally Insane

perlre

$lhs_re = qr/(\w+):(?<name>\w+)/;$rhs_re = qr/(?<value>\w+)/;

Regex: Named Captures

Page 153: Perl 5.10 for People Who Aren't Totally Insane

better backrefs

Page 154: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

m{< (\w+) > .+ < /\1 > }x

Page 155: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

m{< (\w+) > .+ < /\1 > }x

Page 156: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

\10

Page 157: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{(\d)\10}

Page 158: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{ (\d) \1 0}x

Page 159: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

m{< (\w+) > .+ < /\1 > }x

Page 160: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

m{< (\w+) > .+ < /\g{1} > }x

Page 161: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

\10

Page 162: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

\g{10}

Page 163: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{(\d)\10}

Page 164: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{(\d)\g{1}0}

Page 165: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{(\d)\g{-1}0}

Page 166: Perl 5.10 for People Who Aren't Totally Insane

perlre

Regex: Backreference

qr{ (?<digit>\d) \g{digit} 0}x

Page 167: Perl 5.10 for People Who Aren't Totally Insane

wtf

Regex: Exit Strategymy $wtf_re = qr{xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff])|”[^\\\x80-\xff\n\015“]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015”]*)*”)[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff])|”[^\\\x80-\xff\n\015”]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015”]*)*”)[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:”.\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\04};

Page 168: Perl 5.10 for People Who Aren't Totally Insane

perlop

Regex: Backreference

if ($str =~ $wtf_re) { ...}

Page 169: Perl 5.10 for People Who Aren't Totally Insane

perlop

Regex: Backreference

if ($str ~~ $wtf_re) { ...}

Page 170: Perl 5.10 for People Who Aren't Totally Insane

lexically scoped alternate trie-based reentrant user-defined regex pragmata

Page 171: Perl 5.10 for People Who Aren't Totally Insane

flexible regex

Page 172: Perl 5.10 for People Who Aren't Totally Insane

perlreapi

Alternate Regex Engines

Page 173: Perl 5.10 for People Who Aren't Totally Insane

perlreapi

Alternate Regex Engines

- lets you change how regex work

Page 174: Perl 5.10 for People Who Aren't Totally Insane

perlreapi

Alternate Regex Engines

- lets you change how regex work

- but not how you use them (=~, s///, etc)

Page 175: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

Page 176: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

egrep -r -l PATTERN DIR

Page 177: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

my $regex = qr{ ... };

my @files = $find->file->in( $root );

say for grep { slurp ~~ $re } @files;

Page 178: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

Page 179: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

egrep -r -l ‘.+?’ DIR

Page 180: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

my $regex = qr{ ... };

my @files = $find->file->in( $root );

say for grep { slurp ~~ $re } @files;

Page 181: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

use re::engine::POSIX;

my $regex = qr{ ... };

my @files = $find->file->in( $root );

say for grep { slurp ~~ $re } @files;

Page 182: Perl 5.10 for People Who Aren't Totally Insane

re::engine::POSIX

Alternate Regex Engines

use re::engine::POSIX;

my $regex = qr{ ... }x;

my @files = $find->file->in( $root );

say for grep { slurp ~~ $re } @files;

Page 183: Perl 5.10 for People Who Aren't Totally Insane

http://www.perl.org/get.html

Page 184: Perl 5.10 for People Who Aren't Totally Insane

Any questions?


Recommended