I was quite annoyed recently when the Business::CreditCard module didn't return values for cardtype() that I expected. Instead of returning 'Discover' or 'Visa', it returns 'Discover card' and 'VISA card', a fact which you will only know after a short look at the source code.

It wasn't long before I put some constants for the Business::CreditCard in a private module so I wouldn't have this problem again. However, I also think this would be a great place for a junction, if only we didn't have to load all of Quantum::Superpositions or wait for Perl6 to get it.

Instead of returning a single scalar string, we return a junction in a scalar which holds a number of different strings:

return any('Discover card', 'Discover'); # Or . . . return any('VISA card', 'VISA'); # Or maybe . . . return any('American Express card', 'American Express', 'AmEx');

When we check equivilence against the string, any of the values returned will cause the expression to be true. This has the advantage of maintaining backwards compatiblity while returning various values that the user might expect.

I'm sure this isn't limited to the credit card module. The point is that we don't have to limit junctions to just being stuck in a truth test--we can pass them around just like any other scalars.

Update: Fixed a typo.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated


In reply to Returning a Junction by hardburn

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.