Warning this article may contain opinions of the author that you and iTWire don't agree with.
Visit the last page to have your say in our forum.

No. 1 Story

Telstra adds one million mobile services, but Sensis plummets

Telstra has revealed the addition of almost one million new mobile services in the six months to December 2011, but Sensis revenues plummeted 24 percent in 12 months.

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.

Loading comments ...



- sponsored feature -

The Death of Traditional BI: What’s Next?

How to Make Business Discovery Work for Your Business IP PABX BUYING GUIDE

Business Discovery takes its cues from consumer apps. Like Google, it encourages us- ers to hunt for and explore data without worrying about or even noticing the underly- ing technology. Their entire experience is working within an intuitive interface to get real-time, self-service results with only minimal training. ...more