in reply to Re: Short circuits in Logical AND (&&)
in thread Short circuits in Logical AND (&&)

Oh!! I generally dont like to use the "until" block .. but as you said, this may be a situation that's ideal for its use. If in case I'd want to write this with a while block, would this logic given below work?

while(!(($worker1Finished eq "true")&&($worker2Finished eq "true"))) { print "."; sleep(1); }

Am just trying to learn the different cases for using the "while" and "until" constructs ... So, please give me your suggestions/advice.

Replies are listed 'Best First'.
Re^3: Short circuits in Logical AND (&&)
by davido (Cardinal) on Aug 02, 2011 at 06:51 UTC

    Yes, there are a lot of ways to write it correctly, and a lot of incorrect ways too. ;) The one you proposed should work fine. But I would use the lower precedence operators:

    while( not ( $worker1Finished eq 'true' and $worker2Finished eq 'true' ) ) { # ... }

    ...to at least eliminate one set of parens. In fact, if my memory of the precedence tables serves me (which it may not), you could eliminate the other set of parens like this:

    while( not $worker1Finished eq 'true' && $worker2Finished eq 'true' ) { # ... }

    Because 'eq' is higher precedence than &&, and && is higher precedence than 'not', so eq binds first, && second, and 'not' last.

    But the gods gave us 'until(){}' because it suited them to do so. While it's important to use caution when wielding negatives (and double negatives, etc.), it is useful sometimes. My preference in this case is to use it.

    until( $worker1Finished eq 'true' and $worker2Finished eq 'true' ) { # ... }

    Personal preference. But to me "until this and that keep looping" reads better than "while not both this and that keep looping."


    Dave

      Fantastic Explanation! Thanks for your patience .. that answer gave me a very good insight on using conditionals and operators. Yes, your way of eliminating paranths holds as per precedence. Wow.. wondering when I will start thinking like you when I write code... (sigh)