Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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

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

$opt = 1;
switch($opt) {
case 1 : printf "Hi";
case 2 : print "hello";
default : break;

I think everything is correct as per the switch in perl.
But still it throws the error "Bad case statement (invalid case value?) near ./ 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
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:

#!/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:

#!/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
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.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 AM.

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