Hi monks.

I was reading through my old Beginning Perl book to get up to speed on Perl again, when I saw a joke the author had made that there was no elsunless in Perl. This got me wondering: could you use an else block with unless like you could with if, in case the unless failed. My hypothesis was that it wouldn't work, as it seemed....I don't know,counterintuitive. In retrospect, it was a pretty silly hypothesis, but it just didn't feel right that this could work at the time.

So i wrote a little block of code to test it:

#!perl use warnings; use strict; unless(1){ print "Hello\n"; } else{ print "WHAT?!!!\n"; }

...and, to my surprise, it spitted out : WHAT?!!!

This is probably not news to all you old hats at the monastery, but I never realised you could do it, and I wondered how many other new initiates didn't either. So I posted it here just in case.


In reply to unless....else by amrangaye

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.