in reply to Re: Perl 5.10 given/when tricks and caveats
in thread Perl 5.10 given/when tricks and caveats
Here's the new C and Perl code and their output:
Perl code:
#!/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: $i\n"; } match 1; match 2; match 3; match 4; match 5;
C code:
#include <stdio.h> #include <stdlib.h> int match ( int foo ) { int i = 0; switch ( foo ) { case 1: i++; case 2: i++; case 3: i++; case 4: i++; break; } printf( "%d: %d\n", foo, i ); } int main ( int argc, const char **argv ) { match( 1 ); match( 2 ); match( 3 ); match( 4 ); match( 5 ); exit( 0 ); }
And here's the new output for the Perl:
And the C:1: 1 2: 1 3: 1 4: 1 5: 0
1: 4 2: 3 3: 2 4: 1 5: 0
|
|---|