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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Returning a Junction
by diotalevi (Canon) on Sep 03, 2003 at 21:54 UTC | |
by pdcawley (Hermit) on Sep 04, 2003 at 07:17 UTC | |
by hardburn (Abbot) on Sep 04, 2003 at 13:45 UTC | |
by diotalevi (Canon) on Sep 04, 2003 at 15:04 UTC | |
|
Re: Returning a Junction
by belg4mit (Prior) on Sep 03, 2003 at 21:19 UTC | |
|
Re: Returning a Junction
by fletcher_the_dog (Friar) on Sep 04, 2003 at 19:00 UTC | |
by hardburn (Abbot) on Sep 04, 2003 at 19:20 UTC | |
by fletcher_the_dog (Friar) on Sep 04, 2003 at 19:40 UTC | |
by dragonchild (Archbishop) on Sep 04, 2003 at 19:53 UTC |