shloro
shloro

Reputation: 115

How to use a given-when 'OR' in perl?

I'm pretty new in Given-When statement in perl. I would like to get the same result for $var=0 or 2, any help?

I've tried the following but I get error.

given($var) {
when(0 || 2) { print "bala";}
when(1) {print "blabla";}
default {print "default";}
}

Upvotes: 2

Views: 1277

Answers (2)

Andy Tarkinson
Andy Tarkinson

Reputation: 101

You can shorten it up doing this:

given($var) {
print "bala" when [0,2];
print "blabla" when [1];
default {print "default"}
}

Your original didn't work because "0 || 2" always evaluates to "2"

Upvotes: 6

shloro
shloro

Reputation: 115

Got it!

given($var) {
when($_==0 || $_==2) { print "bala";}
when(1) {print "blabla";}
default {print "default";}
}

Upvotes: 0

Related Questions