in reply to Re^3: Evaluate arbitrary boolean expressions
in thread Evaluate arbitrary boolean expressions

OK, that's great but how can I contruct the variable $bool on the fly?

In general I will have a function that will return values 0 and 1. I can then combine these with operators || and &&. However if I write all this to a string variable $bool the test does not work properly!

  • Comment on Re^4: Evaluate arbitrary boolean expressions

Replies are listed 'Best First'.
Re^5: Evaluate arbitrary boolean expressions
by hippo (Archbishop) on Mar 13, 2018 at 11:54 UTC
    the test does not work properly!
    use strict; use warnings; use Test::More tests => 2; my $bool; sub odd { return $_[0] % 2; } $bool = (odd(3) && !odd(4)) || (!odd(3) && !odd(1)); ok ($bool, "First call is true"); $bool = (odd(3) && !odd(5)) || (!odd(3) && !odd(1)); ok (!$bool, "Second call is false");

    "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be specific. SSCCE is best.

Re^5: Evaluate arbitrary boolean expressions
by Corion (Patriarch) on Mar 13, 2018 at 11:44 UTC

    If you are looking at actual functions, consider just using eval, at least in the case where your input comes from a trusted source (and not from the internet).