Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

unless....else

by amrangaye (Friar)
on Aug 05, 2003 at 12:27 UTC ( [id://280954]=perlmeditation: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re: unless....else
by Abigail-II (Bishop) on Aug 05, 2003 at 12:31 UTC
    You can even have:
    unless (...) { ... } elsif (...) { ... } else { ... }

    Abigail, who isn't surprised by something that's clearly documented.

      Update: Please delete/ignore this. It's much too late for me to be typing anything remotely resembling code.

      use strict; use warnings; unless( 1 ) { print "Hello\n"; } unless( 2 ) { print "World\n"; } else { print "WHAT?!!!\n"; } __END__ WHAT?!!!
        Unless I am mistaken you are trying to show that unless the first condition is false or that the second considition is false, then print the stuff in the else statement(kind of akin to an if/elsif/else construct, but for unless). At least that is the jist of what I got from your post (forgive me if I am wrong, but want to make sure that no one is led astray here).

        Anyhow the reason WHAT?!!! is printed is not because neither of the unless conditions were false (and hence executed), but because the second statement's condition (i.e. unless( 2 ){ .. } ), was true (and not executed), and thus the else that follows is executed. For example:

        will print both "TEST 1" and "TEST 3"

        -enlil

Re: unless....else
by jeffa (Bishop) on Aug 05, 2003 at 13:40 UTC
    I am going out on a limb here, but i believe you misread the author's comment. The author said there is no elsunless, not that you can't have an else block to catch a failed unless block. For example, you can't say this in Perl:
    unless ($login and $pass) { print "need login and pass"; } elsunless (valid($pass)) { print "invalid password"; } else { print "logged in"; }
    You would have to replace the elsunless block with:
    elsif (!valid($pass)) {
    Note that this example should not be used as a model for login/pass validation, by the way. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: unless....else
by AcidHawk (Vicar) on Aug 05, 2003 at 13:10 UTC
Re: unless....else
by derby (Abbot) on Aug 05, 2003 at 12:49 UTC
    Why the big suprise. unless is just syntactic sugar for if not. In the traditional manner, you should read if statements as

    if condition is true do clause

    so you should read unless statements as

    if condition is not true do clause

    (of course perl lets you write those as do ... if too)

    So in your example you're saying if 1 is not true, print hello. Since 1, by definition, is true, you'll always print "WHAT?!!!\n".

    -derby

    update Damn ... just re-read the OP and realized the big suprise wasn't that it didn't print "Hello" but that you could place an "else" block after "unless." The syntactic sugar part still holds (its just an if (! condition)) but sorry about the rest.

Re: unless....else
by DrHyde (Prior) on Aug 05, 2003 at 13:01 UTC
    I've known that unless ... else ... works for ages, but I don't use it for just the reason you didn't think it would work - it just doesn't sound right. When someone else is reading and maintaining my code I want them to be able to read it quickly and easily, not have to keep going "huh?".

      When I see unless (cond) {code} else {other code}, something wierd happens in my head. I know it works, I know what it does, but I can't get what's happening unless I mentally make it if (not cond) {code} else {other code}. Call me crazy (you won't be the first), but that's how my mind works. (Does this just mean I'm getting old?)

      So since I'm going to do this in my head anyhow, that's how I write it.

      What's strange is that I use it in modifiers all the time, e.g. next unless $_;. No problem for me here.

      --Bob Niederman, http://bob-n.com
Re: unless....else
by rnahi (Curate) on Aug 05, 2003 at 13:43 UTC

    unless is just one of the nice operators in Perl to make your program more readable, like checksomething or dosomethingelse.

    See also Statement Modifiers, Whaa? for more interesting aspects of unless.

Re: unless....else
by bunnyman (Hermit) on Aug 05, 2003 at 20:31 UTC
    The elsif is just a shorter way of saying:
    if(1) { print 'true' } else { if(1) { print 'false, true' } }
    and so you could write this:
    unless(1) { print 'false' } else { unless(1) { print 'true, false' } }
    But it would be pretty wierd to use that in real life code.
Re: unless....else
by Maclir (Curate) on Aug 07, 2003 at 12:56 UTC
    Now all we need is a "comefrom" statement - the opposite of the goto.
    mylabel: some statement ... ... much further down the program ... comefrom mylabel;
    That would be way cool.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://280954]
Approved by Corion
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-29 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found