LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2010, 12:35 PM   #1
kavil
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Perl switch statement throwing error like Bad case statement (invalid case value?)


Hi
I've written a simple perl code to learn switches in perl.
My code is pasted below,

#!/usr/bin/perl
$opt = 1;
switch($opt) {
case 1 : printf "Hi";
break;
case 2 : print "hello";
break;
default : break;
}

I think everything is correct as per the switch ..case in perl.
But still it throws the error "Bad case statement (invalid case value?) near ./switch.pl line 4"

Can you please help me to come out of this error.
Thanks in advance.

Last edited by kavil; 10-06-2010 at 12:36 PM.
 
Old 10-06-2010, 08:48 PM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
What version of Perl are you using? So far as I know, the Switch module is going to be removed from core Perl soon.

In any case, here's a working switch block:

Code:
#!/usr/bin/env perl
use strict;
use warnings;
use Switch;

my $opt = 1;

switch($opt) {
    case 1 { print "It's a 1\n" }
    case "1" { print "Uh oh\n" }
    else { print "Won't get here...\n" }
}
Note that if you run this you will get a warning, "Switch will be removed from the Perl core distribution in the next major release. Please install it from CPAN. It is being used at <script name>, line 4."

Alternatively, don't use Switch at all. Use Perl's relatively recent given...when construction:

Code:
#!/usr/bin/env perl
use strict;
use warnings;
use feature ":5.12";

my $opt = 1;

given($opt) {
    # optional fall through with continue
    when(1)     { print "It's a 1\n"; continue }
    when("1")   { print "It's also a '1'\n" }
    when(1)     { print "No fall through above. Won't see this one\n" }
    default     { print "Won't get here...\n" }
}

Last edited by Telemachos; 10-06-2010 at 09:01 PM.
 
Old 10-07-2010, 05:50 AM   #3
kavil
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for your kind reply..Its working fine now.
Thanks a lot.

Last edited by kavil; 10-07-2010 at 05:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Perl Code : Bad switch statement(Problem in code block)? near ## line # suyog255 Programming 4 02-20-2008 06:35 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 09:05 PM
case statement baks Programming 2 03-15-2007 02:19 PM
Does CGI/PERL have a Select Case Statement? craig467 Programming 2 02-16-2007 05:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration