Hi Monks!!!
Ok, this might seem like a noob question but I'm confused on Perl's behaviour. I have code that checks 2 flags, whether they are "true" or not. Until both of them are set to "true", my script is required to sleep and wait for them. Here's my code:
print "Waiting for all workers to finish their tasks ..."; while (($worker1Finished ne "true") && ($worker2Finished ne "true")) { print "."; sleep(1); }
Well, as you punters might have guessed, I'm having problems with the way Perl "short circuits" my logical AND (&&) operator in the while condition.
According to perldoc.perlop, Binary "&&" performs a short-circuit logical AND operation. That is, if the left operand is false, the right operand is not even evaluated. Scalar or list context propagates down to the right operand if it is evaluated. So, in my code, if worker 1 has not yet finished, the script will wake up before it is supposed to...and this is resulting in hara-kiri!
The functionality of my code is such that either worker1 can finish first, or worker 2... there is no "predefined" order in which these flags are set to "true". How do I ensure that both flags are checked for "true" and only after this, the execution moves forward ???
In reply to Short circuits in Logical AND (&&) by vishi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |