Pacific-Design.com

    
Home Index

1. Perl5

2. Smart Match

Perl5 / Smart Match /

New Fetures in Perl 5.10

#---------------------- Smart Match -------------------#
use feature '~~';
if($url ~~ @array)     { say "$x exists" }
if($url ~~ /google/)   { say "google is in the string" }
if(@array ~~ /google/) { say "google is in the array" }
if($key ~~ %hash)      { say "$key exists in the hash" }
if($subref ~~ %args)   { say 'sub($args) true' }

#----------------------- Defined Or ------------------#
$c = $a // $b is handler
    Same As 
$c=defined($a) ? $a : $b;

#--------------------- State Variable ----------------#
use feature 'state';
sub incrementor {
    state $i = 0;
    return $i++;
}

#-------------------- Switch Statement ---------------#
use feature 'switch';
foreach(@array) {
    when(/google/) { $google++ }
    when(/yahoo/)  { $yahoo++  }
    when(/bing/)   { $bing++   }
    say "$_ non of the above ...";
}

Reference:
http://www.slideshare.net/acme/whats-new-in-perl-510
http://search.cpan.org/~dapm/perl-5.10.1/