|P is for Practical|
Re^3: if variantsby ihb (Deacon)
|on Oct 21, 2004 at 14:37 UTC||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.
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.
can be read
"unless X" can be read "if not X". Combining this gives you
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.
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.
See perltoc if you don't know which perldoc to read!