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.


Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy

In reply to Re: if/unless and chaining by BrowserUk
in thread if/unless and chaining by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.