What's new in Perl 5.10.1

[ Perl tips index ]

[ Subscribe to Perl tips ]

In late August, Perl 5.10.1 was officially released. In this tip, we'll examine what's changed between Perl 5.10.0 and 5.10.1.

In addition to this Perl tip, you can read the Perl 5.10.1 delta document that describes the changes in more detail.

Bug-fixes and optimisations

Perl 5.10.1 is primarily a maintenance release, which means a large collection of bug-fixes, optimisations, additional tests and tuning. In particular, 5.10.1 runs significantly faster than 5.10.0 with many common subroutine calls.

If you're already using 5.10.0, you may find that upgrading to 5.10.1 will give your code a significant speed boost.

Range operators

Perl's .. operator is now always interpreted as being in boolean context when used inside a when statement. In practical terms, this now means that structures like the following now work:

while (<>) { given ($_) { when ( /START_HEADER/ .. /END_HEADER/ ) { next; } # Skip header when ( /^\s*#/ ) { next; } # Skip comments default { process_line($_); } } }

Under Perl 5.10.0, the header skipping code above would never trigger, however under 5.10.1 it now works as intended. To use given/when to match a range, an array reference should be used (in both 5.10.0 and 5.10.1):

given ($number) { when ( [1..10] ) { say "In range" } default { say "Out of range" } }

Smart-match

Perl 5.10.1 introduces significant changes to the ~~ , the smart-match operator. The most important changes are detailed below:

Better encapsulation

Smart-match no longer works on objects unless they have overloaded the smart-match operator. This prevents smart-match for breaking encapsulation and poking around in the internals of your objects when you don't want it to. Objects that don't overload smart-match, but do overload stringification or numerification, will be treated as strings or numbers, respectively.

No longer commutative

In Perl 5.10.0, the ordering of the operands to smart-match did not matter. In 5.10.1, they do matter, with the rightmost argument primarily determining behaviour.

The following common idioms work in both 5.10.0 and 5.10.1:

$key ~~ %hash # Does $key exist in %hash ? $value ~~ @array # Does $value exist in @array?

But be careful! The following are now always false in 5.10.1:

%hash ~~ $key @array ~~ $value

In a given/when construct, the given argument is always considered to be the left operand, and the when argument is always considered to be the right operand.

The new smart-match table can be found in perlsyn .

Distributive

Where possible, the new smart-match is distributive, and will apply itself recursively across large data structures. For example, the following expression is true when evaluated under 5.10.1, but false under 5.10.0:

'bar' ~~ [ 'foo', [ 'bar', 'baz' ], 'qux' ]

In the same vein, given the following code:

[ 'foo', 'bar', 'baz' ] ~~ \&subroutine

Perl 5.10.1 will call the subroutine three times (once for each element), and the whole expression will be considered true if all the results are true. On the other hand, Perl 5.10.0 would call the subroutine only once, passing in the whole data structure.

autodie

Perl 5.10.1 includes the autodie pragma as part of the distribution, meaning it can now be used out-of-the-box in new code. Using autodie causes Perl's built-ins to succeed or die, meaning you can write code like the following:

use autodie; chdir($some_dir); open(my $fh, '<', $some_file); # Process records close($fh);

If the chdir , open or close functions fail, they'll automatically throw an exception, removing the tiresome need to add or die... guards to these lines.

We've discussed autodie extensively in a previous Perl tip.

[ Perl tips index ]

[ Subscribe to Perl tips ]

This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

If you have any questions please don't hesitate to contact us:

Email: contact@perltraining.com.au Phone: 03 9354 6001 (Australia) International: +61 3 9354 6001

Copyright Perl Training Australia. Contact us at contact@perltraining.com.au