Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me fix the syntax error in the below code? This is my first time using given/when and it just says syntax error hear ") and syntax error near ") {" which makes no sense to me.
while(1) { print "input: "; my $in = <STDIN>; chomp($in); given($in) { when (/l/) { print "\nlegs"; } when (/h/) { print "\nhead"; } when (/b/) { print "\nbody"; } when (/s/) { print "\nspecial"; } } }

Replies are listed 'Best First'.
Re: Quick syntax error in 13 lines of code
by toolic (Bishop) on Sep 30, 2011 at 14:15 UTC
Re: Quick syntax error in 13 lines of code
by GrandFather (Saint) on Oct 01, 2011 at 03:19 UTC

    You have to turn on new features in Perl 5.10. Toolic indicated one way. Another way is to turn on all the 5.10 features so the usual strictures boilerplate extends to include use 5.010;:

    use strict; use warnings; use 5.010; while (1) { print "input: "; my $in = <STDIN>; chomp ($in); given ($in) { when (/l/) {print "\nlegs";} when (/h/) {print "\nhead";} when (/b/) {print "\nbody";} when (/s/) {print "\nspecial";} } }
    True laziness is hard work