On 10/15/07, Johan Meskens CS3 jmcs3 snip > > I *wish* it was 5.10 as that has some > > very nice features that I am excited about. > > like what for example ? snip
Here are some of the things that are coming in Perl 5.10. * a switch statement: given($string) { when (/^abc/) { $abc = 1; } when (/^def/) { $def = 1; } when (/^xyz/) { $xyz = 1; } default { $nothing = 1; } } * The // and err operators (like || and or, but tests for defined rather than truth) my $sleep = shift // 10; #does not overwrite 0 like (shift || 10) would * alternations in regexes are significantly more efficient due to the use of tries * recursive regexes It is now possible to write recursive patterns without using the "(??{})" construct. This new way is more efficient, and in many cases easier to read. Each capturing parenthesis can now be treated as an independent pattern that can be entered by using the "(?PARNO)" syntax ("PARNO" standing for "parenthesis number"). For example, the following pat‐ tern will match nested balanced angle brackets: / ^ # 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 aga in )* # 0 or more times. > # match a closing angle bracket ) # end capture buffer one $ # end of line /x * named regex captures It is now possible to name capturing parenthesis in a pattern and refer to the captured contents by name. The naming syntax is "(?<NAME>....)". It's possible to backreference to a named buffer with the "\k<NAME>" syntax. In code, the new magical hashes "%+" and "%-" can be used to access the contents of the capture buffers. * relative backreferences A new syntax "\g{N}" or "\gN" where "N" is a decimal integer allows a safer form of back-reference notation as well as allowing rela‐ tive backreferences. This should make it easier to generate and embed patterns that contain backreferences. See "Capture buffers" in perlre. (Yves Orton) * "\K" escape The functionality of Jeff Pinyan's module Regexp::Keep has been added to the core. You can now use in regular expressions the spe‐ cial escape "\K" as a way to do something like floating length pos‐ itive lookbehind. It is also useful in substitutions like: s/(foo)bar/$1/g that can now be converted to s/foo\Kbar//g which is much more efficient. (Yves Orton) * UNITCHECK blocks "UNITCHECK", a new special code block has been introduced, in addition to "BEGIN", "CHECK", "INIT" and "END". "CHECK" and "INIT" blocks, while useful for some specialized purposes, are always executed at the transition between the compilation and the execution of the main program, and thus are useless whenever code is loaded at runtime. On the other hand, "UNITCHECK" blocks are executed just after the unit which defined them has been compiled. See perlmod for more information. (Alex Gough) * the smart match operator (~~) The behaviour of a smart match depends on what type of thing its arguments are. It is always commutative, i.e. "$a ~~ $b" behaves the same as "$b ~~ $a". The behaviour is determined by the following table: the first row that applies, in either order, determines the match behaviour. $a $b Type of Match Implied Matching Code ====== ===== ===================== ============= (overloading trumps everything) Code[+] Code[+] referential equality $a == $b Any Code[+] scalar sub truth $b->($a) Hash Hash hash keys identical [sort keys %$a]~~[sort key s %$b] Hash Array hash value slice truth grep $_, @[EMAIL PROTECTED] Hash Regex hash key grep grep /$b/, keys %$a Hash Any hash entry existence exists $a->{$b} Array Array arrays are identical[*] Array Regex array grep grep /$b/, @$a Array Num array contains number grep $_ == $b, @$a Array Any array contains string grep $_ eq $b, @$a Any undef undefined !defined $a Any Regex pattern match $a =~ /$b/ Code() Code() results are equal $a->() eq $b->() Any Code() simple closure truth $b->() # ignoring $a Num numish[!] numeric equality $a == $b Any Str string equality $a eq $b Any Num numeric equality $a == $b Any Any string equality $a eq $b + - this must be a code reference whose prototype (if present) is not "" (subs with a "" prototype are dealt with by the 'Code()' entry lower down) * - that is, each element matches the element of same index in the other array. If a circular reference is found, we fall back to referential equality. ! - either a real number, or a string that looks like a number * state variables (like static in C) * the say function (like print, but with a newline)