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: Intermediate:
Switch statement problem

 



revme
New User

Sep 29, 2006, 10:29 AM

Post #1 of 3 (313 views)
Switch statement problem Can't Post

hi,

I am trying to use a switch statement on the following, and the program abruptly ends with

Bad switch statement (problem in the code block?) near ./abc.pl

it works if i remove the -t case .


switch ($input) {

case "-o" {
my @result = $obj->compress_ip_list(@test);
foreach (@result) { print $_ . "\n"; }
}
case "-i" {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) { print $_ . "\n"; }

}
case "-t" {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) { print $_ . "\n"; }

}

case "-d" {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) {
print FR $_ . "\n"
}
}


KevinR
Veteran


Sep 29, 2006, 12:39 PM

Post #2 of 3 (312 views)
Re: [revme] Switch statement problem [In reply to] Can't Post

the switch module appears to be a bit buggy or at least hard to get working well, I would go with if/else blocks or a hash instead.

Name your subroutine switch if you want to and then:


Code
sub switch { 
my $input = shift;

if ($input eq "-o") {
my @result = $obj->compress_ip_list(@test);
foreach (@result) { print $_ . "\n"; }
}

elsif ($input eq "-i") {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) { print $_ . "\n"; }
}

elsif ($input eq "-t") {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) { print $_ . "\n"; }
}

elsif ($input eq "-d") {
my @result = $obj->get_valid_static_ip_range($CUSTOMER_RANGE, @test );
foreach (@result) {
print FR $_ . "\n"
}

else {
print "Bad input, no results for '$input'\n";
}
}


$obj, $CUSTOMER_RANGE, and @test have to be accessible inside the sub routine if you code it like that. I assume they are judging by the code you posted.
-------------------------------------------------


revme
New User

Sep 29, 2006, 12:45 PM

Post #3 of 3 (308 views)
Re: [revme] Switch statement problem [In reply to] Can't Post

I was copying the contents of one of the case statements to create another case and I think during that process some characters got inserted which were not liked by switch.

I typed the case and then it worked.

Thanks

Rev

 
 


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

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