given($foo) {
when (/x/) { say '$foo contains an x'; continue }
when (/y/) { say '$foo contains a y' }
default { say '$foo contains neither an x nor a y' }
}
####
#!/usr/local/bin/perl
use feature qw{ switch };
use strict;
use warnings;
sub match {
my $i = 0;
my $foo = shift;
given ( $foo ) {
when ( 1 ) { $i++; continue; }
when ( 2 ) { $i++; continue; }
when ( 3 ) { $i++; continue; }
when ( 4 ) { $i++; }
}
print "$foo: ";
print "4!" if 4 == $i;
print "\n";
}
match 1;
match 2;
match 3;
match 4;
match 5;
####
1:
2:
3:
4:
5:
####
#include
#include
int match ( int foo ) {
int i = 0;
printf( "%d: ", foo );
switch ( foo ) {
case 1: i++;
case 2: i++;
case 3: i++;
case 4: i++;
break;
}
if ( 4 == i ) {
printf( "4" );
}
printf( "\n" );
}
int main ( int argc, const char **argv ) {
match( 1 );
match( 2 );
match( 3 );
match( 4 );
match( 5 );
exit( 0 );
}
####
1: 4
2:
3:
4:
5:
####
#!/usr/local/bin/perl
use feature qw{ switch };
use strict;
use warnings;
sub match {
my $i = 0;
my $foo = shift;
print $foo . ': ';
given ( $foo ) {
when ( /^Just / ) { $i++; continue; }
when ( /another / ) { $i++; continue; }
when ( /[Pp]erl / ) { $i++; continue; }
when ( /hacker\.?$/ ) { $i++; }
}
print 'Me too!' if 4 == $i;
print "\n";
}
match 'Just another Perl hacker';
match 'Just another Perl slacker';
####
Just another Perl hacker: Me too!
Just another Perl slacker:
####
$foo contains an x
$foo contains neither an x nor a y