Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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


In reply to Behavior of Flip-Flop Operators by haukex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found