Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

It's worth noting, by the way, that using $foo != 7 as the control statement in an if statement is generally a Bad Idea. The unless statement is designed to cover that need, and using the != operator there just makes the code harder to read later when it must be maintained.

I second DrHyde (Re^3: if variants). It's definately not bad and neither is it a maintenance trouble. That whole sentence above should be removed from your text. It's just plain wrong.

The unless keyword is made to make you write code more like a natural language. It's not there to take !='s place in if expressions. They can both coexist happily and be mixed however you please.

Why the combination of unless and else is shunned by many is because it's a sort of double negation.

if (X) { ... } else { ... }
can be read
if (X) { ... } if (not X) { ... }
"unless X" can be read "if not X". Combining this gives you
unless (X) { ... } else { ... } if (not X) { ... } if (not not X) { ... }
which just doesn't read well. Logically it's not strange but it's a funny and inappreciated way of expressing oneself.

My own (rather inconsistent) style is to use unless for exceptional behaviour, and if for expected/wanted behaviour. When using unless I usually want something, but something might stop me from doing that. With if I express that I perhaps want something, perhaps not.

print ... unless $quiet; # I want to print. exit if $done; # I might want to exit here. exit unless $stay; # I want to exit here, but apparently # something is holding me back.
The examples can perhaps be better, and I'm not consistant myself in my use, but that's give you an idea of how I tend to use them.

ihb

See perltoc if you don't know which perldoc to read!
Read argumentation in its context!


In reply to Re^3: if variants by ihb
in thread if variants by apotheon

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (13)
    As of 2015-07-07 19:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls