Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

First Look: Perl 5.10 is a Pearl

perl.jpgAs most Perl fans are no doubt aware, the Perl Foundation released version 5.10 last month and introduced a number of significant upgrades for the popular programming language. Perl 5.10 is the first significant feature upgrade since the 5.8 release back in 2002.

First the good news, AKA why you should go ahead and upgrade: the major new language features are turned off by default which means you can upgrade without breaking existing scripts, and take advantage of the new features for new scripts. Even cooler is ability to progressively upgrade scripts using the “use” syntax.

For instance, add the line use feature 'switch'; prior to a block of code where you’d like to take advantage of the new switch statement in Perl 5.10 and then turn it off after upgrading that block of code using the statement no feature 'switch';. New features can be enabled by name or as a collective group using the statement use feature ':5.10';.

In addition to the switch statement, there’s a new say statement which acts like print() but adds a newline character and a state feature, which enables a new class of variables with very explicit scope control.

But perhaps the most interesting of 5.10’s new features is the new ‘or’ operator, //, which is a “defined or” construct. For instance the following statements are syntactically equivalent:

$foo // $bar

defined $foo ? $foo : $bar

Obviously the first line is much more compact and (I would argue) readable — i.e. is $foo defined? If not, give it the value $bar.” You can also add an equal sign like so:

$bar //= $foo;

Which is the same as writing:

$bar = $foo unless defined $bar;

Another noteworthy new feature is the smart match operator, which the Perl Foundation explains as “a new kind of comparison, the specifics of which are contextual based on the inputs to the operator.” For example, to find if scalar $needle is in array @haystack, simply use the new ~~ operator

if ( $needle ~~ @haystack ) ...

Perl 5.10 also finally gains support for named regex statements, which means you can avoid the dreaded lines of $1 $2 etc, which often make Perl regex hard to decipher. Finally I might be able to understand what’s going on in complex regex scripts like Markdown.

Other improvements include a faster interpreter with a smaller memory footprint, better error messages and more. For full details on the new release check out the notes.

I’ll confess I abandoned Perl for Python some time ago, but after playing with 5.10 I may have to rethink that decision, Perl 5.10’s new features are definitely worth the upgrade and a must have for anyone who uses Perl on a daily basis.

See Also:

Post Comment Comments Permalink Print
Reddit Digg

 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year