Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Logic expression evaluation not working. What am I doing wrong?

by Anonymous Monk
on Jan 04, 2016 at 02:29 UTC ( [id://1151799]=note: print w/replies, xml ) Need Help??


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

safety first
my %allowed = qw{ ( ( ) ) ! !! & && }; my $finalExpr = map { $allowed{$_} || die "not allowed '$_'" } split ' +', $expr; my $result = eval $finalExpr;

Replies are listed 'Best First'.
Re^2: Logic expression evaluation not working. What am I doing wrong?
by mr_ron (Chaplain) on Jan 04, 2016 at 16:30 UTC
    #from Anonymous Monk code > my %allowed = qw{ ( ( ) ) ! !! & && }; > my $finalExpr = map { $allowed{$_} || die "not allowed '$_'" } split + '', $expr;

    The %allowed hash doesn't look right and it isn't, among other things because it forgets digits [0-9]. A solution along these lines might do something like using taint mode for the script and then:

    print "EXPR = ",$newExpr,"\n"; # don't know how to execute shell commands without [A-Za-z] letters $newExpr =~ /^([\d\s()&|!]+)$/; printf "%b\n", eval $1; }
    Ron
      The %allowed hash doesn't look right ...

      I would be more concerned about the map statement:

      c:\@Work\Perl>perl -wMstrict -le "my $expr = '(!(C)&T)&!Q'; my %allowed = qw{ ( ( ) ) ! !! & && C 1 T 1 Q 1 }; my $finalExpr = map { $allowed{$_} || die qq{not allowed '$_'} } spli +t '', $expr; print qq{'$finalExpr'}; " '11'


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

        I would be more concerned about the map statement:

        :D I was hoping the OP would be concerned when trying, so he can get a link to Basic debugging checklist

        Then add the missing  join '',

      Why would boolean logic need more than 1 and 0?

      If using %allowed all the values are already untainted

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found