CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
given/when not working

 



droidus2
Novice

Nov 12, 2015, 5:41 PM

Post #1 of 6 (1625 views)
given/when not working Can't Post

Here is my code:

Code
given ( $beginUserInput ) 
{
when ( 1 ) { print "1\n"; }
default { print "Other...\n"; }
}


I am getting this error:

Code
syntax error at ./script.pl line 39, near ") 
{"
Execution of ./script.pl aborted due to compilation errors.


Any ideas on how to resolve this?


BillKSmith
Veteran

Nov 12, 2015, 6:04 PM

Post #2 of 6 (1621 views)
Re: [droidus2] given/when not working [In reply to] Can't Post

The switch feature is still experimental. Details depend on what version of perl you are using. You probably need one or more use statements. Refer to perldoc perlsyn to learn how to use it with your version of perl.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Nov 13, 2015, 1:56 AM

Post #3 of 6 (1615 views)
Re: [BillKSmith] given/when not working [In reply to] Can't Post

You probably need a statement like this one:

Code
use v5.14;


(or some higher version of Perl) to get a clean compile, but please note, as already mentioned, that this given ... when construct has been downgraded to a "highly experimental" feature status:

Quote
As previously mentioned, the "switch" feature is considered highly experimental; it is subject to change with little notice. In particular, when has tricky behaviours that are expected to change to become less tricky in the future. Do not rely upon its current (mis)implementation.


See http://perldoc.perl.org/perlsyn.html#Experimental-Details-on-given-and-when for more details.

I personally would not use this feature for any code of lasting value.


droidus2
Novice

Nov 13, 2015, 3:35 AM

Post #4 of 6 (1612 views)
Re: [Laurent_R] given/when not working [In reply to] Can't Post

What would you recommend that I use instead? What about case?


Laurent_R
Veteran / Moderator

Nov 13, 2015, 3:54 AM

Post #5 of 6 (1611 views)
Re: [droidus2] given/when not working [In reply to] Can't Post

Perhaps simply this:


Code
if ( $beginUserInput == 1) {  
print "1\n";
} else {
print "Other...\n";
}


Or possibly:


Code
print $beginUserInput == 1 ? "1\n" : "Other...\n";


But it really depends on the exact semantics you were giving to the given expression. May be you really wanted something like this:


Code
print $beginUserInput =~ /1/ ? "1\n" : "Other...\n";



BillKSmith
Veteran

Nov 13, 2015, 5:17 AM

Post #6 of 6 (1607 views)
Re: [droidus2] given/when not working [In reply to] Can't Post

The O'Reilly book "Perl Best Practices" suggests a very clever extension to Laurent's last method:

Code
use strict; 
use warnings;
my $average_test_score = 76;

my $letter_grade
= $average_test_score > 89 ? 'A'
: $average_test_score > 79 ? 'B'
: $average_test_score > 69 ? 'C'
: $average_test_score > 59 ? 'D'
: 'F'
;

print $letter_grade;

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives