Wednesday, January 26, 2011

Perl - given/when sample with strings

From Perl 5.10, We can use given/take for what is usually known as switch/case statement:
Here a sample with given/take using strings:
use strict;
use warnings;
use feature qw(switch);
my $name = "Monday";
given ($name) {
 when ("Sunday") { print "first\n"}
 when ("Monday") { print "second\n"}
 when ("Tuesday") { print "third\n"}
 default { print "None\n" }

print "Bye\n";
the "use feature qw(switch)" line tell the compiler to use Perl6's given/when switch feature

No comments: