📜 ⬆️ ⬇️

What's new in Perl 5.10?

A few days ago, the first Perl candidate came out for release - 5.10.0 RC1 . But 5 years have passed since the previous release (5.8).

So what's waiting for us at 5.10?

It should be noted that Perl 5.10 is backward compatible with previous versions, unlike the upcoming Perl 6.
')
By default, new features of version 5.10 are disabled. They are activated by the special pragma use feature , which, by the way, has lexical scope; or using the write record 5.10.

Say function
Analogue of the good old print, with the only difference that say automatically adds "\ n" to the output line:
#
print "$_\n" for @lines;
#
say for @lines;


Operator //
Mix || and defined, which is called defined-or. It is known that the following code is not entirely correct, since the "false" values ​​"" and 0 are not taken into account:
$var = $_[0] || 'default';
The correct entry looks like this:
$var = $_[0];
$var = 'default' unless defined $var;
#
$var = defined $_[0] ? $_[0] : 'default';

Thanks to the new operator // the previous two examples are being rewritten like this:
$var = $_[0];
$var //= 'default';
#
$var = $_[0] // 'default';


Operator ~~
This operator implements a smart matching mechanism or smart matching. It is commutative (that is, $ a ~ ~ $ b is equivalent to $ b ~~ $ a) and its behavior depends on the types of arguments passed. For example:
say "$x " if $x ~~ @array; # grep $_ == $b, @$a grep $_ == $b, @$a
say "foobar " if @x ~~ /foobar/; # grep /$b/, @$a
say "$key " if $key ~~ %hash # exists $a->{$b}

In other words, ~~ is all-in-one. Such a context-sensitive shortcut. The full table of behaviors is available by reference from the header.

given - analogue of the C-construction switch
Now this construction is built-in. It is implemented by three new words: given, when, and default. In addition, it uses the clever mapping described above. An example in the studio!
#
if( $var ~~ undef ) { ... }
elsif( $var ~~ $var ) { ... }
elsif( $var ~~ @array ) { ... }
elsif( $var ~~ %hash ) { ... }
elsif( $var ~~ /regexp/ ) { ... }
else { ... }
#
given( $var ) {
when( undef ) { ... }
when( $var ) { ... }
when( @array ) { ... }
when( %hash ) { ... }
when( /regexp/ ) { ... }
default { ... }
}

Game "Guess the number" (:
use feature qw( switch say );
my @guessed;
my $num = int( rand 100 ) + 1;
while( my $guess = ) {
chomp $guess;
given( $guess ) {
when( /\D/ ) { say ", " }
when( @guessed ) { say " " }
when( $num ) { say " !"; last }
when( $_ < $num ) { say ""; continue }
when( $_ > $num ) { say ""; continue }
push @guessed, $_;
}
}


. (?...). — \k. %+ . %-, , , .
# name=value
$str =~ /(?\w+)=(?\w+)/;
say "The value of $_ is $+{$_}" foreach keys %+;
(possessive quantifiers). - , . « » , «?» «+». : ?+, *+, ++, {min,max}+.


state-
C- static. state , .
sub counter {
state $i = 0;
return $i++;
}


Filetest -X

-x $file && -w _ && -f _

-f -w -x $file


. , perl5100delta.pod

:
Perl 5.10 for People Who Aren't Totally Insane What's new in Perl 5.10?

Source: https://habr.com/ru/post/16556/


All Articles