Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: backslash found where operator expected at

by ikegami (Patriarch)
on Apr 21, 2021 at 21:23 UTC ( [id://11131555]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found