in reply to if/unless and chaining
Given possible states A,B,C,D and the if-elsif-else structure
if( state equals A) { Take action1 } elsif( state equals B) { Take action2 } else { Take action3 }
any given state will cause exactly 1 action to be taken no matter what the value of state.
However using this
unless( state equals A ) { Take action1 } elsunless( state equals B ) { Take action2 } else { Take action3 }
If the state is C or D, by the logic of the statements, actions 1 and 2 would need to be taken; but as the entire block terminates once one condition succeeds, action2 would only ever be performed if the state was A, which is counter-intuative to say the least. Also, when would the else clause be performed?
Effectively, the structure above is equivalent to
unless( state equals A) { Take action1 } elsif ( state equals A) { Take action2 } else { Take action3 }
which is already legal in Perl, but is actually the same as
unless( state equals A) { Take action1 } else { Take action2 }
As action three will never be taken as every state except A will have caused action 1 and then the contruct terminates.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: if/unless and chaining
by John M. Dlugosz (Monsignor) on Nov 06, 2002 at 03:43 UTC | |
by Anonymous Monk on Nov 06, 2002 at 20:23 UTC |