Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Behavior of Flip-Flop Operators

by haukex (Bishop)
on May 29, 2016 at 15:09 UTC ( #1164447=perlmeditation: print w/replies, xml ) Need Help??

Hello everyone,

A recent thread reminded me of a script I wrote years ago while learning about the flip-flop operator (aka the range operator in scalar context), and I thought I'd share it in case it helps someone else.

The script, which I've included below the output, runs through a sequence of true/false values, tests whether the right-hand-side and/or left-hand-side of the operator is evaluated (marked by an asterisk in the table), gets the return value of the operator, and outputs all that in the following handy table. I hope it illustrates the difference between the .. (two-dot) and ... (three-dot) versions of the operator: two dots will immediately evaluate the RHS if the LHS is true, three dots will wait until the next evaluation.

*** Demonstration of the Flip-Flop Operators *** __A______B____X___ __A______B____X___ 0* .. 0 = 0* ... 0 = 0* .. 1 = 0* ... 1 = 0* .. 0 = 0* ... 0 = 1* .. 0* = 1 1* ... 0 = 1 0 .. 0* = 2 0 ... 0* = 2 0 .. 0* = 3 0 ... 0* = 3 0 .. 1* = 4E0 0 ... 1* = 4E0 0* .. 0 = 0* ... 0 = 0* .. 0 = 0* ... 0 = 1* .. 1* = 1E0 1* ... 1 = 1 0* .. 0 = 0 ... 0* = 2 0* .. 0 = 0 ... 0* = 3 1* .. 0* = 1 1 ... 0* = 4 0 .. 0* = 2 0 ... 0* = 5 0 .. 1* = 3E0 0 ... 1* = 6E0 0* .. 1 = 0* ... 1 = 1* .. 1* = 1E0 1* ... 1 = 1 1* .. 0* = 1 1 ... 0* = 2 0 .. 0* = 2 0 ... 0* = 3 0 .. 1* = 3E0 0 ... 1* = 4E0 0* .. 0 = 0* ... 0 = 1* .. 1* = 1E0 1* ... 1 = 1 1* .. 1* = 1E0 1 ... 1* = 2E0 1* .. 1* = 1E0 1* ... 1 = 1 0* .. 0 = 0 ... 0* = 2 0* .. 1 = 0 ... 1* = 3E0 0* .. 0 = 0* ... 0 = (* = Evaluated)
#!/usr/bin/env perl use warnings; use strict; print "*** Demonstration of the Flip-Flop Operators ***\n\n"; my @tests = qw/ 0:0 0:1 0:0 1:0 0:0 0:0 0:1 0:0 0:0 1:1 0:0 0:0 1:0 0:0 0:1 0:1 1:1 1:0 0:0 0:1 0:0 1:1 1:1 1:1 0:0 0:1 0:0 /; print join(' ', ("__A______B____X___") x 2), "\n"; sub fmt { my ($lv,$le,$op,$rv,$re,$v) = @_; return sprintf ' %s%s %-3s %s%s = %-3s ', $lv, $le?'*':' ', $op, $rv, $re?'*':' ', $v; } for (@tests) { my ($lv, $rv) = split /:/; my ($le, $re); my $lf = sub { $le=1; $lv }; my $rf = sub { $re=1; $rv }; ($le, $re) = (0,0); my $v1 = &$lf .. &$rf; print fmt($lv,$le,'..' ,$rv,$re,$v1); print ' '; ($le, $re) = (0,0); my $v2 = &$lf ... &$rf; print fmt($lv,$le,'...',$rv,$re,$v2); print "\n"; } print "\n(* = Evaluated)\n";

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re: Behavior of Flip-Flop Operators
by GrandFather (Sage) on May 29, 2016 at 21:08 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1164447]
Approved by Athanasius
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-10-24 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?