in reply to Logic expression evaluation not working. What am I doing wrong?

Building on AnonyMonk's approach above (and this actually works):

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "use constant OPS => qw{ ( ( ) ) ! ! | || & && }; ;; sub compile { my ($expr, $hr_vars) = @_; ;; my %xlate = (OPS, %$hr_vars); ;; $expr =~ s{ (.) } { exists $xlate{$1} or die qq{bad: '$1' at offset $-[1] in '$expr'}; $xlate{$1}; }xmsge; ;; return $expr; } ;; my %vars = qw(Q 0 T 1 C 0); dd \%vars; ;; my $expr = '(!(C)&T)&!Q'; print qq{initial expr: '$expr'}; ;; my $final_expr = compile($expr, \%vars); print qq{ final expr: '$final_expr'}; ;; my $result = eval $final_expr; if ($@) { print qq{eval of '$final_expr' failed: $@} } else { print qq{'$final_expr' -> '$result'}; } " { C => 0, Q => 0, T => 1 } initial expr: '(!(C)&T)&!Q' final expr: '(!(0)&&1)&&!0' '(!(0)&&1)&&!0' -> '1'
Some thoughts:


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Logic expression evaluation not working. What am I doing wrong?
by Anonymous Monk on Jan 04, 2016 at 04:56 UTC

    "The expression !1 evaluates to ''" I tried the following:
    #!/usr/bin/perl use strict; use warnings; printf "%b\n", !1;
    which output the following:
    0
    on my Linux machine. So, not sure under what circumstances, like you say that it outputs '' empty string.
    Can you elaborate a bit for novices like myself please?
    TIA.

      I was thinking of the following contexts | examples:

      c:\@Work\Perl>perl -wMstrict -le "print '>', !1, '<';" >< c:\@Work\Perl>perl -wMstrict -le "print '>', !1 && 1, '<';" ><
      In numeric contexts such as that supplied by the  %b printf format specifier, !1 will, I think, always evaluate to numeric 0 — but it would be best to do some research on this!


      Give a man a fish:  <%-{-{-{-<