The Linux distillery
Bringing the world of Linux to you, David cuts through the tech and shows you how it works and how to use it, in terms that apply to any distro. RSS
IT NEWS     Sustainability    Get JOB ALERTS that match your skills
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 E-mail
User Rating: / 13
PoorBest 
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.

Please enable JavaScript in your browser to post your comment!


Get stories like this delivered daily - FREE - subscribe now


 
< Next story in category   Previous story in the category >
  •   *  
  • Search
  • AdvSeach
  • Login
  • Events
  • FreeStuff
Subscribe to our free e-newsletter
First name:
Last name:
Your email address:
Your role:
Your industry:
Australian state:
Country:
Enter the security code shown:
mandatory
Contact , Register , Advertise with iTWire , Links , About iTWire , Feedback , Post your jobs , Events , iTWire site map , Start Blogging , MyBlogLog page
Industry Releases , Submit your release now