Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: if variants

by Chady (Priest)
on Oct 19, 2004 at 11:53 UTC ( #400480=note: print w/ replies, xml ) Need Help??


in reply to if variants

regarding the precedence, you can also use the and and or variants who have lower precendence so that you can have assignements work.

Here's a rough example with nested ifs and an else

if ($i > 1 ) { if ($a = function_that_may_fail()) { $i++; } else { $i = 1; } }

can become:

$i > 1 and $a = function_that_may_fail() and $i++ or $i = 1;

He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
Chady | http://chady.net/
Are you a Linux user in Lebanon? join the Lebanese Linux User Group.


Comment on Re: if variants
Select or Download Code
Re^2: if variants
by apotheon (Deacon) on Oct 19, 2004 at 11:56 UTC
    Ack! I forgot to mention parentheses! I've got to go add slightly to my "Appendix" section, now.

    - apotheon
    CopyWrite Chad Perrin
Re^2: if variants
by SpanishInquisition (Pilgrim) on Oct 19, 2004 at 16:37 UTC
    Personally I like:

    if ($i>1) { $a = function_that_may_fail(); $i = ($a) ? $i+1 : 1; }

    I find that the only time I really use "and" and "or" are with carp/croak/warn/die. But that's just me.

    At worst:

    do { $a=function_that_may_fail(); $i= $a ? $i+1 : 1; } if ($i>1)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-08-29 10:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls