NAME
Math::Logic::Ternary::Expression - ternary logic on native perl expressions
VERSION
This documentation refers to version 0.004 of Math::Logic::Ternary::Expression.
SYNOPSIS
use Math::Logic::Ternary::Expression qw(:all);
$foo = not3( $bar);
$foo = and3( $bar, sub { $baz });
$foo = or3( $bar, sub { $baz });
$foo = xor3( $bar, sub { $baz });
$foo = eqv3( $bar, sub { $baz });
$foo = bool3($bar);
$val = val3($foo);
DESCRIPTION
This module provides ternary logic operations on native perl expressions. Ternary truth values are 1 (or anything evaluating in Perl as true, except unblessed code references), the empty string (or anything evaluating in Perl as false but defined), and undef.
As in Perl built-in logic, result values in general represent the value of the last operand evaluated, not just plain boolean values. By way of some additional magic, negated terms recover their original value when they are negated once more. To discard any information but the truth value, use bool3 rather than double negation.
To make short-cut expression evaluation possible, operands can be wrapped in code references, which will lazily be evaluated as late as possible (if at all). If you do want to treat an unblessed code reference as true, wrap it in one additional code reference before using it as a ternary logical operand.
All subroutines except val3 return an expression object and accept arbitrary Perl scalars as well as expression objects as arguments. The actual value for use outside of ternary logical expressions is returned by val3.
Expression objects can be stored in variables and be used more than once, acting as common subexpressions. Code reference wrappers will be executed at most once, even then.
Unary Operators
- val3
-
This operator recovers an ordinary Perl scalar from an expression object returned by any of the other operators. If the operand is not an expression object, it is returned itself.
- not3
-
A | val3(not3(A)) | val3(not3(not3(A))) -------+---------------+--------------------- true | not A | A false | not A | A undef | undef | undef
- bool3
-
A | val3(bool3(A)) -------+---------------- true | 1 false | '' undef | undef
Binary Operators
- and3
-
A | B | val3(and3(A, B)) -------+-------+------------------ true | true | B true | false | B true | undef | undef false | true | A false | false | A false | undef | A undef | true | undef undef | false | B undef | undef | undef
- or3
-
A | B | val3(or3(A, B)) -------+-------+----------------- true | true | A true | false | A true | undef | A false | true | B false | false | B false | undef | undef undef | true | B undef | false | undef undef | undef | undef
- xor3
-
Unlike perl-builtin xor, xor3 is implemented with short-cut behaviour and value preservation. The second operand will only be evaluated if the first one is defined. If one operand is true and the other operand is false, the true operand will be the result. Exclusive-or is defined based on other operators like this:
A xor B := !(A and B) and (A or B)
A | B | val3(xor3(A, B)) -------+-------+------------------ true | true | not B true | false | A true | undef | undef false | true | B false | false | B false | undef | undef undef | true | undef undef | false | undef undef | undef | undef
- eqv3
-
Ternary logical equivalence eqv3 is defined very similar to xor3, swapping and and or:
A eqv B := !(A or B) or (A and B)
A | B | val3(eqv3(A, B)) -------+-------+------------------ true | true | B true | false | B true | undef | undef false | true | A false | false | not B false | undef | undef undef | true | undef undef | false | undef undef | undef | undef
EXPORT
By default, nothing is exported. Subroutine names can be imported individually or via the :all tag.
SEE ALSO
AUTHOR
Martin Becker, <becker-cpan-mp@cozap.com>
COPYRIGHT AND LICENSE
Copyright (C) 2012-2017 by Martin Becker, Blaubeuren. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.