Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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..."


Comment on Re: A TRUE-once variable
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://129790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-12-27 23:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls