(Updated to add explanation of the error in the scalar test.)

I added tests for code references and scalars as conditions:

sub c3 { print "Evaluating condition 3\n"; /gamma/; } sub c4 { print "Evaluating condition 4\n"; /omega/; } while (<DATA>) { chomp; mff(qr/alpha/ => \&s1, qr/beta/ => \&s2, \&c3 => \&s3, c4() ) or print "*:$_\n"; }

In the above, a code ref to c3 is passed. c4, however, is called and its result, a scalar, is passed.

The results:

Evaluating condition 4 *:This is Evaluating condition 4 1:the alpha Evaluating condition 4 1:but not Evaluating condition 4 1:the omega Evaluating condition 4 2:Now the beta Evaluating condition 4 Evaluating condition 3 2:progressing to Evaluating condition 4 Evaluating condition 3 *:the gamma Evaluating condition 4 *:and finally Evaluating condition 4 *:the omega Evaluating condition 4 *:Did this work?

c3, which was passed as a code ref, was evaluated only in state 2, as expected. So, conditions passed as code refs do work.

c4, because it is called before calling mff, is evaluated every iteration, as expected. However, there is an error: mff transitioned to the "inactive" state too soon.

Update: changing the call to c4 to !!c4() "fixed" the test. The !! imposes scalar context on the call to c4. (It also "imposes" boolean context. Note that +c4() or -c4() would also have worked.)

Note: LanX raises a valid point: "What if a test returns a reference?" That depends on what the reference points to. If a scalar, then the scalar value is tested. If a regex, it is evaluated against $_. If code, it is evaluated and its return value tested. If another reference, that reference is treated as a scalar.1 If anything else, mff croaks with the message "Unsupported type".

I think flip-flop ( .. ) handles refs to scalars, regexen and code the same. Given that ref(ref(any)) eq 'SCALAR', I suspect it handles refs to refs the same as scalars. Refs to anything else, I don't know. More testing needed.

---

1 I tested print ref(ref(1)) and print ref(ref(qr/foo/)) and print ref(ref(sub { print 1; })) and several others. Each time, the result was SCALAR


In reply to Re^4: Multi-stage flip-flop? ( till() - proof of concept) by RonW
in thread Multi-stage flip-flop? by RonW

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.