sillymonkeysoftw...@gmail.com <sillymonkeysoftw...@gmail.com> asked:
> I'm working with some customizable data from a config file, and can't > for the life of me figure out how to test conditionals from an > external source. > Does anyone have an idea how to accomplish something like: > > my $statement = "1 < 10 and 2 > 10"; # obviously false > my $result = ($statement) ? 1 : 0; # conditional test > > Now, $result will always be true(1), because the conditional is > really, in effect, just validating that $statement is a defined > scalar, not testing the statement. > But, how can I perform an actual conditional statement on this? > What I really want is: > > (1 < 10 and 2 > 10) ? 1 : 0; You can do something like my $cond = eval( $statement ); if( $@ ){ # an error occurred while evaluation $statement # } else { # $statement was o.k.; normal program flow my $result = eval($statement) ? 1 : 0; # rest of code } The two important things to keep in mind: 1) Make sure you sanitize $statement very carefully. Otherwise people can run arbitrary perl commands and you probably don't want that. 2) Check $@ after calling eval to see wether the code could be executed or wether it caused an error. HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/