Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Evaluate arbitrary boolean expressions

by roboticus (Chancellor)
on Mar 13, 2018 at 11:00 UTC ( [id://1210798]=note: print w/replies, xml ) Need Help??


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

gauss76:

if ($bool) { print "It's true\n"; } else { print "It's false\n"; }

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^4: Evaluate arbitrary boolean expressions
by gauss76 (Scribe) on Mar 13, 2018 at 11:30 UTC

    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!

      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.

      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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1210798]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found