Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: A TRUE-once variable

by baku (Scribe)
on Dec 06, 2001 at 03:10 UTC ( #129790=note: print w/replies, xml ) Need Help??

in reply to A TRUE-once variable

Well, *I* never use it, because I have to run  man perlop every time I see one, but...

In scalar context, ``..'' returns a boolean value. The operator is bistable, like a flip-flop ... It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once.

Anybody who can get it to work, though? I can't get what I expect from, e.g.

 $\ = ', '; for (1..10) { print if (1..0) }

Rhandom is right, revising the algorithm is likely the way to go, but I'd like to learn how to use this as a cool way to write "the first time..."

Replies are listed 'Best First'.
Bi-stable operator as a true-once value.
by clintp (Curate) on Dec 06, 2001 at 19:33 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129790]
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2017-07-22 13:29 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (339 votes). Check out past polls.