Warning this article may contain opinions of the author that you and iTWire don't necessarily agree with. Don't let them get away with it - have your say with a comment!

No. 1 Story

HP job cuts loom for Australian employees

A number of Australian employees of Hewlett-Packard are facing the loss of their jobs as the global computer giant looks to slash its worldwide workforce by up to 30,000.

read more

Legends of FOSS: Perl 5.10 is here

Opinion and Analysis

Defined-or operator
Ever one for obscure operators, Perl 5.10 also introduces the // operator, which means defined-or. This operator fundamentally provides a shortcut to test if a variable is defined, and if not to give it a value – but if it is defined, leave it be.

For instance

$a // $b

is equivalent to

defined $a ? $a : $b

That is, “is $a defined? If not, give it the value $b.” Additionally, the statement

$c //= $d;

is syntactically equivalent to

$c = $d unless defined $c;

That is, “is $c defined? If not, give it the value $d.”

Recursive regular expression patterns
Not one for the faint-hearted, regular expressions – or regexs – are a mainstay of Computer Science and an essential component of string handling.

Perl 5.10 makes it easier to write recursive patterns – ie ones that can backtrack over themselves and try multiple paths. To illustrate, consider the following regex pattern which matches nested balanced angle brackets. By nested, we mean the string will contain multiple occurrences of the brackets, and by balanced we mean for each opening angle bracket there is a closing one too.

/
     ^                      # start of line
     (                      # start capture buffer 1
        <                   #   match an opening angle bracket
        (?:                 #   match one of:
            (?>             #     don't backtrack over the inside of this group
                [^<>]+      #       one or more non angle brackets
            )               #     end non backtracking group
        |                   #     ... or ...
            (?1)            #     recurse to bracket 1 and try it again
        )*                  #   0 or more times.
        >                   #   match a closing angle bracket
     )                      # end capture buffer one
     $                      # end of line
    /x


Whoa nelly. That example may require some thinking to get your head around, but ultimately it operates on a single line of text. It searches for opening or closing angled brackets – ie < and > - and finds a match for each one but by progressively working on itself over and over to match the inward pairs of brackets before the outward pairs.

It’s undoubtable: it took a long time to come, but the latest Perl is a pearler. And with the new introduction of the feature pragma Perl scripters can breathe a collective sigh of relief knowing their scripts will continue to work and that the task of upgrading to the eventual goal of Perl 6 will not be an arduous one. Well done, Perl Foundation.