An interesting way to do it is to use a full blown parser. Marpa is a relatively easy way to get the job done:

use strict; use warnings; use Marpa::R2; package DoStuff; my %vars; sub doAnd { my @params = @_; return $params[1] && $params[3]; } sub doOr { my @params = @_; return $params[1] || $params[3]; } sub doVarValue { my @params = @_; return $vars{$params[1]}; } sub doAssign { my @params = @_; $vars{$params[1]} = $params[3]; return; } sub doVal { my @params = @_; my $val = 'ARRAY' eq ref $params[1] ? $params[1][1] : $params[1]; return $val; } sub doNVal { my @params = @_; my $val = 'ARRAY' eq ref $params[2] ? $params[2][1] : $params[2]; return $val ? 0 : 1; } sub doResult { my @params = @_; return $params[-1]; } package main; my $syntax = <<'SYNTAX'; lexeme default = latm => 1 test ::= assignments expression action => doResult | expression action => doResult assignments ::= assignment assignments | assignment assignment ::= var '=' constant action => doAssign expression ::= expression '|' term action => doOr | term action => ::f +irst term ::= term '&' factor action => doAnd | factor action => ::first factor ::= negatable action => doVal | '!' negatable action => doNVal negatable ::= '(' expression ')' action => [values] | value action => +::first value ::= var action => doVarValue | constant action => ::first var ~ [A-Z] constant ~ [10] :discard ~ spaces spaces ~ [\s]+ SYNTAX my $grammar = Marpa::R2::Scanless::G->new({source => \$syntax}); for my $inputs ([0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 0, 1]) { my $input = <<INPUT; C = $inputs->[0] Q = $inputs->[1] T = $inputs->[2] (!(C)&T)&!Q INPUT my $result = $grammar->parse(\$input, 'DoStuff'); printf "${input}Result: %d\n\n", $$result; }

Prints:

C = 0 Q = 0 T = 0 (!(C)&T)&!Q Result: 0 C = 0 Q = 0 T = 1 (!(C)&T)&!Q Result: 1 C = 0 Q = 1 T = 1 (!(C)&T)&!Q Result: 0 C = 1 Q = 0 T = 1 (!(C)&T)&!Q Result: 0
Premature optimization is the root of all job security

In reply to Re: Logic expression evaluation not working. What am I doing wrong? by GrandFather
in thread Logic expression evaluation not working. What am I doing wrong? by perl_learner_2016

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.