Top Banner
Perl v5.26 New Features AmsterdamX.pm, 8 Jun 2017 Tunisian Camels (2010), from Ray on Flickr
20

Perl v5.26 Features (AmsterdamX.pm)

Jan 21, 2018

Download

Technology

_brian d _foy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Perl v5.26 Features (AmsterdamX.pm)

Perl v5.26New

FeaturesAmsterdamX.pm, 8 Jun 2017

Tunisian Camels (2010), from Ray on Flickr

Page 2: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

scalar %hash

Page 3: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

The key count• A hash in scalar context returned “hash

statistics”my %h = (a=>1, b=>2); say scalar %h; # ‘2/8’

• Everyone expected it to be the number of keys• Now that’s what they get.my %h = (a=>1, b=>2); say scalar %h; # 2

Page 4: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

POSIX::tmpnam

Page 5: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

use File::Temp qw/ tempfile tempdir /;

$fh = tempfile();($fh, $filename) = tempfile();

($fh, $filename) = tempfile( $template, DIR => $dir);

($fh, $filename) = tempfile( $template, SUFFIX => '.dat');

($fh, $filename) = tempfile( $template, TMPDIR => 1 );

Page 6: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

\{

Page 7: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Regexes are messier• \N{NAME}, \x{ABCD}, \p{PROPERTY}, \P{PROPERTY}, \b{…}, \B{…}, a{MIN,MAX}, and so on

• New things are probably on the way• So, every { is now special and not literal• Some old tools might break

Page 8: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

/xx

Page 9: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

More Meaninglessness• /x makes most whitespace in a regex

insignificant• And, you can add comments inside regex• Except in a character class [ a b c ]• Or [ a #comment b c ]• Previously, Perl ignored additional /x• Maybe you added /xxx to honor Amsterdam.• Now you can’t.• /xx now applies the rules inside a char class

Page 10: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Apple Part Numbermy $string = ' Z007LL/A';my $pattern = qr/ \A # M = First sale, F = Refurbished, P = Personalized [ M F P ] # probably not all these letters here [ A-Z ] \d+ (?: # country codes LL | [ FBEJTXYC ] ) \/ [ A B C ] # revision \z /xx; say $string =~ $pattern ? 'Matched' : 'Missed';

Page 11: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Hopes for /xxx(?:L [ A # Colombia, Ecuador, El Salvador, … Peru E # Argentina L # US Z # Chile, Paraguay, Uruguay ] | [ F # France B # Ireland, UK E # Mexico J # Japan T # Italy X # Australia, New Zealand Y # Spain C # Canada ] )

Page 12: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Indented Heredocs

Page 13: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

my $string = <<~“HERE"; This is the first line This is the second line HERE

This is the first lineThis is the second line

Page 14: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

my $string = <<~“HERE"; This is the first line This is the second line HERE

This is the first line This is the second line

Page 15: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

No . in @INC

Page 16: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

More Securitiness• What’s that . anyway?• This doesn’t mean that you are safe• CPAN tested, but not DarkPAN tested• do warns when it fails to load a file from .• You can disable this, but I won’t tell you how

Page 17: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

@{^CAPTURE}

Page 18: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

List of Captures• %{^CAPTURE} is %+ (named captures)• %{^CAPTURE_ALL} is %- (all named captures)• @{^CAPTURE} is the cool one.• ${^CAPTURE}[INDEX] is the numbered capture

minus 1

Page 19: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

sub match_it_1 ( $rx, $string ) {$string =~ $rx;say "$1•$2•$3"; # Hoping there's three}

sub match_it_2 ( $rx, $string ) {$string =~ $rx;say join '•', map {

substr $string, $-[$_], $+[$_]-$-[$_]} 1 .. $#-;

}

sub match_it_3 ( $rx, $string ) {$string =~ $rx;local $" = '•';say "@{^CAPTURE}";}

Page 20: Perl v5.26 Features (AmsterdamX.pm)

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Questions