[ Perl tips index ]
[ Subscribe to Perl tips ]
The Perl release schedule has been sped up, with the goal of releasing a major stable release of Perl 5 every year. So 5.12.0 came out in 2010, 5.14.0 in 2011 and 5.16.0 just a couple of months ago. Point releases are also more frequent, occurring about every 3-4 months, as needed. Development releases (5.15.x, 5.17.x, etc) are scheduled monthly on the 20th.
This increase in the release schedule means that the differences between the major revisions are a lot smaller than they used to be. The changes between 5.0053 and 5.6.0 were huge, as were the changes between 5.6 and 5.8. The changes between 5.8.8 and 5.8.9 were almost as great as the changes between 5.8.8 and 5.10!
Now that these changes are smaller, it should make upgrading to newer versions of Perl less scary. If you're still using 5.8.x please consider jumping forward to 5.14 or 5.16.
Now that there's a release schedule, there's also an official deprecation schedule. Bug fixes and security patches will not be back-ported further than last year's version. So if you're using 5.8.x, you're not only missing out on great new features and improvements, but security issues are not getting fixed - or at least not for free.
Now that 5.16 has come out, the only supported versions of Perl are 5.14 and 5.16.
As well as adding in new things, the more recent versions of Perl are removing some old cruft. Things that are going include:
Switch. This module attempted to allow switch-like semantics for Perl. It
was buggy and has been replaced with given and when that came in with
5.10.
Devel::Dprof. This module does basic profiling, but Devel::NYTProf
from the CPAN is a whole lot better!
In order to (attempt to) preserve backwards compatibility, to get Perl's
great new features you need to ask for them. While you can request
features explictly with the use feature pragma:
use feature qw(switch say state unicode_strings
unicode_eval evalbytes current_sub array_base fc);
It's much easier to simply specify the target version of Perl you wish to use. All the relevant features for that version will be turned on automatically:
use v5.16;
All of Perl's new features are lexically scoped, so they'll only be enabled for the file, block, or eval in which you enable them. Let's have a look at some of these features now:
These all came in with 5.10 and have been covered in earlier Perl tips.
switch provides a super-powerful switch statement for Perl using
given and when. say gives us print with a newline. state
gives us C-style static variables, which means we can avoid having to
create closures to achieve the same purpose.
You can read more about these in our previous Perl tips:
This pragma instructs Perl to use full Unicode-semantics in strings and regular expressions. This helps avoid the "Unicode Bug".
It is recommended that you use this pragma in all programs that are not explicitly working with ASCII.
unicode_eval allows you to instruct Perl to treat any string eval's
argument as a string of Unicode characters.
The evalbytes function will evaluate a given string as bytes (not
unicode), regardless of if the unicode_eval feature is on.
That is:
eval $string; # $string is evaluated as bytes
use feature qw(unicode_eval evalbytes);
eval $string; # $string is evaluated as Unicode
evalbyes $string; # $string is evaluated as bytes
current_sub provides a new token, __SUB__, which is a reference to
the subroutine it has been used inside. This is particularly useful if you
need to define recursive subroutines. See
brian d foy's
post on __SUB__ for further
information.
If you write use v5.16, then this disallows changing the long deprecated
global variable $[. Use the array_base feature if you really need to
get this functionality back.
fc (available in 5.16 onwards) provides a new function fc (fold case)
which allows for Unicode case-folding. Use fc instead of lc or uc
when using Unicode characters, especially when comparing. For example, fc
will correctly understand that greek has two forms of the lower-case sigma
character (one of which only occurs at the end of words):
use utf8;
use v5.16;
my $upper_sigma = 'Σ';
my $lower_sigma = 'σ';
my $end_lower_sigma = 'ς';
if( fc($lower_sigma) eq fc($end_lower_sigma) ) {
# They're the same!
}
From 5.12 and onwards, specifying the version of Perl you wish to use will automatically turn on strictures for you.
use v5.12; # includes use strict;
In addition to named captures, named sub-patterns and all-or-nothing repetitions that came in 5.10, 5.14 brought in non-destructive substitutions. This is one of our favourite reasons for using Perl 5.14+.
Previously if you were walking through a simple template you might have written code like this:
my $string = "Dear NAME,\n\nPlease join me for dinner on Friday night.".
"\n\nLove me";
foreach my $friend (@friends) {
my $message = $string;
$message =~ s/NAME/$friend/;
send($message);
}
or even:
foreach my $friend (@friends) {
(my $message = $string) =~ s/NAME/$friend/;
send($message);
}
However if you make an error with the placement of those parentheses, then rather than copying the string and then modifying it, you can end up modifying the original string itself!
In this case what we really want is a way to do the substitution, but to
then return the change, rather than modify the original string. Perl 5.14
provides a new modifier /r which does exactly this. Now we can write:
foreach my $friend (@friends) {
my $message = $string =~ s/NAME/$friend/r;
send($message);
}
or even:
foreach my $friend (@friends) {
send( $string =~ s/NAME/$friend/r );
}
In 5.12 and above, all file handles are automatically blessed into the
IO::File class, and you can use IO::File methods on them whenever you
need to. As IO::File inherits from IO::Handle this means that all
file handles have these methods, for example we can control buffering when
we need to:
use v5.14;
use autodie;
open my $fh, ">", $file;
# flush to disk right now:
$fh->flush;
# Turn on auto-flushing
$fh->autoflush(1);
# Turn off auto-flushing
$fh->autoflush(0);
# Turn on auto-flushing for STDOUT
STDOUT->autoflush(1);
There are many places to explore to learn more about these features. The following lists some of them:
The perldelta document (perldoc perldelta) lists all the major
changes between your current version of Perl and the previous one. They
are well worth have a looking through.
To find out more about the feature pragma and pointers to what it includes,
read
perldoc feature.
The best places to find out more about Perl's regular expressions,
including new syntax are the perlre,
perlretut
and re
perldoc pages.
[ 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 2001-2012 Perl Training Australia. Contact us at contact@perltraining.com.au