in reply to backslash found where operator expected at

Don't use Switch! Nonsense errors like this is why.

Replace

switch ($response_code) { case '200' { # OK print_response $response_content; exit 0; } case '400' { exit 1; } # BAD_INPUT case '403' { exit 31; } # SERVICE_ACCESS_ERROR case '500' { exit 32; } # SERVICE_EXECUTION_ERROR case '503' { exit 30; } # SERVICE_ERROR else { exit -1; } }
with, for example,
for ($response_code) { if ($_ == 200) { # OK print_response $response_content; exit 0; }; $_ == 400 and exit 1; # BAD_INPUT $_ == 403 and exit 31; # SERVICE_ACCESS_ERROR $_ == 500 and exit 32; # SERVICE_EXECUTION_ERROR $_ == 503 and exit 30; # SERVICE_ERROR exit 255; }

Note that I replaced exit(-1) with exit(255) since -1 isn't a valid exit code (at least not on Windows, Linux and macOS).

Seeking work! You can reach me at ikegami@adaelis.com

Replies are listed 'Best First'.
Re^2: backslash found where operator expected at
by perlfan (Vicar) on Apr 22, 2021 at 05:32 UTC
    In the spirit of TIMTOWTDI:
    my $handles = { 400 => 1, # BAD_INPUT 403 => 31, # SERVICE_ACCESS_ERROR 500 => 32, # SERVICE_EXECUTION_ERROR 503 => 30, # SERVICE_ERROR }; for ($response_code) { if ($_ == 200) { # OK print_response $response_content; exit 0; }; if (defined $handles->{$_}) { exit $handles->{$_}; } }
    $handles could also be a CODE refs that do something, then exit:
    my $handles = { 200 => sub { print_response $response_content; 0}, # OK! 400 => sub { 1}, # BAD_INPUT 403 => sub {31}, # SERVICE_ACCESS +_ERROR 500 => sub {32}, # SERVICE_EXECUT +ION_ERROR 503 => sub {30}, # SERVICE_ERROR }; for ($response_code) { if (defined $handles->{$_}) { # check for 'CODE' ref up to you exit $handles->{$_}->(); } }

      I love dispatch tables. Definitely something to consider in such situations. I simply didn't think them particularly clear/clean/useful here.

      Seeking work! You can reach me at ikegami@adaelis.com