This is derived from an example in the Perl Developer's Dictionary and it's a cool trick if you can stomach it.

$a=0; $_=0; while($_++<10) { if (! $a..(!$a)) { print "True!\n"; } else { print "False!\n"; } $a++; }
The if with the .. operator there returns true exactly once (so long as $a isn't externally modified) for the entire life of the program.

You can make the example a bit more concise using this:

$a=0; $_=0; while($_++<10) { if (! $a..(!$a++)) { print "True!\n"; } else { print "False!\n"; } }
But using an auto-increment in an expression where the target variable appears twice makes me nauseous. Probably indigestion from my C days.

In reply to Bi-stable operator as a true-once value. by clintp
in thread A TRUE-once variable by gmax

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.