0

This program throws error for redo how could i solve this

use strict;
use warnings;
use Switch::Plain;

my %data = (0 => "Enter the number",1 => "UPC",2 => "URL", 3 => "Elastic Search", 4 => "API", 5 => "MONGO", 6 => "TSV", 7 => "SQL", 8 => "JSON", 9 => "Enter the correct value");
my $input = "Enter the number:";


sub input(){
    print "Choose You Input Method"."\n";
    print "1.UPC"."\n";
    print "2.URL"."\n";
    print "3.Elastic Search"."\n";
    print $input;
    PROMPT: {
    chomp(my $input = <>);
     nswitch($input){
        case 1 : {print "UPC"."\n"}
        case 2 : {print "URL"."\n"}
        case 3 : {print "Elastic Search"."\n"}
        default: {"Enter the correct value"; redo PROMPT }

      }


}
}

input();

my $pinput = "Enter the number:";
sub pinput(){
    print "Choose Your Process Method"."\n";
    print "1.API"."\n";
    print "2.Mongo"."\n";
    print $pinput;
    $pinput = <>;
    chomp($pinput);
    nswitch($pinput){
       case 1 : {print "API"."\n"}
       case 2 : {print "MONGO"."\n"}
       default : {print "Enter the correct value"."\n"}
}
}

pinput();

The error thrown is

Argument "" isn't numeric in numeric eq (==) at framework.pl line 21, <> line 1

The idea is to repeat the subroutine if there is any mismatch in used input for example if user types the empty value or 4?

Please help me to get solved?

Miller
  • 34,962
  • 4
  • 39
  • 60

2 Answers2

2

You can convert input to number by adding 0, or multiplying by 1,

$pinput = <> *1;

or convert it after chomp

$pinput *= 1;
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • For a empty value it works but i have only 3 switch cases if the user enters 4 then it should not take him to next sub routine it should repeat the same function? @mpapec –  Mar 27 '14 at 12:51
  • @user3231692 not sure what you're referring to, but if `4` is not covered by your cases, it defaults to `default:` – mpapec Mar 27 '14 at 12:54
  • Yeah but it prints befault value and it passes to the next function but i want that to print the default function and exit the function @mpapec –  Mar 27 '14 at 12:56
  • @user3231692 `$data{$input}` will give `API` for input `4` – mpapec Mar 27 '14 at 13:02
0

Check out How do I create a switch or case statement?

You can do that using builtin methods if you're using 5.10 or newer. Then one solution would be to treat what you're matching against as strings instead of numbers:

use 5.010;

use strict;
use warnings;

print "Choose You Input Method"."\n";
print "1.UPC"."\n";
print "2.URL"."\n";
print "3.Elastic Search"."\n";

PROMPT: {
    chomp(my $input = <>);
    given ( $input ) {
        when( '1' )  { say "UPC" }
        when( '2' )  { say "URL" }
        when( '3' )  { say "Elastic Search" }
        default      { print "Enter the correct value"; redo PROMPT }
    };
}
Miller
  • 34,962
  • 4
  • 39
  • 60