perl v5.26 features (amsterdamx.pm)

Post on 21-Jan-2018

307 Views

Category:

Technology

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Perl v5.26New

FeaturesAmsterdamX.pm, 8 Jun 2017

Tunisian Camels (2010), from Ray on Flickr

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

scalar %hash

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

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

POSIX::tmpnam

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 );

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

\{

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

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

/xx

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

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';

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

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Indented Heredocs

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

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

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

No . in @INC

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

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

@{^CAPTURE}

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

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}";}

The Perl Review • www.theperlreview.com

Perl v5.26 New Features

Questions

top related