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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.