The Linux distillery
Bringing the world of Linux to you, this blog will cut through the tech and show you how it works and how to use it, in terms that apply to any distro. RSS
Australian IT JOBS : Sydney IT jobs, UNIX jobs, Linux jobs, Java jobs, ASP jobs Linux.conf.au Linux.conf.au
Technology news and Jobs arrow The Linux distillery arrow Legends of FOSS: Perl 5.10 is here
Legends of FOSS: Perl 5.10 is here PDF Print E-mail
Written by David M Williams   
Wednesday, 02 January 2008
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.



Get stories like this delivered daily - FREE - subscribe now
When you subscribe get a 12 months license for LiveProject
Valued at $99 USD


LiveWire - Desktop alerts Download the FREE iTWire desktop alert widget LiveWire - Desktop alerts


Del.icio.us!


 
< Prev   Next >
Contact , Register , Advertise with iTWire , Links , Register , About iTWire , Feedback , Post your jobs , Events , iTWire site map , Start Blogging
Industry Releases , Submit your release now , Start submitting to iTWire , How to post video