>>>>> "sc" == sillymonkeysoftw...@gmail com <sillymonkeysoftw...@gmail.com> >>>>> writes:
sc> I'm working with some customizable data from a config file, and can't sc> for the life of me figure out how to test conditionals from an sc> external source. sc> Does anyone have an idea how to accomplish something like: sc> my $statement = "1 < 10 and 2 > 10"; # obviously false sc> my $result = ($statement) ? 1 : 0; # conditional test that will never work as is. perl doesn't interpret $statement as code. if it did, how would you ever keep plain data from be run as code? havoc ensues. there are several way sc> Now, $result will always be true(1), because the conditional is sc> really, in effect, just validating that $statement is a defined sc> scalar, not testing the statement. no, it is testing the string for perl's boolean truth. it is false if the value is 0, '0', '' or undef. so this is always true. sc> But, how can I perform an actual conditional statement on this? sc> What I really want is: sc> (1 < 10 and 2 > 10) ? 1 : 0; there are several ways. the worst and what you should not do is use eval. ok, now that is out of the way, you can do it with code references, dispatch tables and various other tricks. my question is why do you need to do this at this level? can you use plain data to describe your conditions like this: my %conds = ( test1 => { min => 1, max => 10, }, test39 => { min => 10, max => 2, }, ) ; then pass in the test name as a key, and do some simple code to compare. note that your silly code was all constants, you need to have some outside value passed in to make sense of this. so write a simple sub that takes a value and 1 or more test key names and tries those on the value. if you need more complex tests, use code refs and make this a mixed dispatch table (mixing standard test entries and custom code entries). ok, to repeat DO NOT USE EVAL. and don't try to have perl run data as code as you try to do. uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/