Sandra Schlichting
Sandra Schlichting

Reputation: 25986

Ways to get rid of empty if-statements?

To me this code is how I think of the problem I want to solve

if ($b eq "" && $ok) {

} elsif ($b eq "" && !$ok) {
    print "ERROR1\n";

} else {
    print "ERROR2\n";
}

but it is not very pretty I suppose having an empty if-statement.

Are there ways to avoid this?

Upvotes: 2

Views: 256

Answers (2)

Jithin
Jithin

Reputation: 2604

Keeping the if, but checking $b

if ($b eq "") {
    if (!$ok) {
        print "ERROR1\n";
    }
}
else{
    print "ERROR2\n";
}

Upvotes: 5

Egg Vans
Egg Vans

Reputation: 944

You just need to change the logic to what you want

if($b eq "" && !$ok){
    print "ERROR1\n";
}elsif( !$ok || $b ne ""){
    print "ERROR2\n";
}

Upvotes: 3

Related Questions