tags:

views:

124

answers:

2

The content of condition.conf:

condition1=$a>$b

Example Perl code:

$cnd_ConfFile = $ARGV[0];
open(CONDITIONS, '<', $cndConfFile);

$cndCount=0;
while ( <CONDITIONS> ) {
        chomp; # no newline
        s/#.*//; # no comments
        s/^\s+//; # no leading white
        s/\s+$//; # no trailing white
        next unless length;
        ($var, $value) = split(/\s*=\s*/, $_, 2);
        $cndOnCounterValues[$cndCount++]=$value;
}

close CONDITIONS;

$cond = $cndOnCounterValues[0];
print "\n$cond\n";

$a=3;
$b=5;

if($cond){
  print "a is greater then b";
}
else
{
  print "b is greater then a";
}

The above code always gives the output "a is greater then b".

Regardless of the values of $a and $b.

+1  A: 

I assume that you want to eval the $a>$b expression that literally appears in your config file. To do that replace:

if ($cond) {

with:

if (eval $cond) {

That should to the trick.

Disclaimer: don't do this unless you know what you are doing (see comments).

Inshallah
The fundamental question, always, concerns the difference between the quick answer to OP's misguided question versus good programming practice. Your answer falls on the wrong side of that comparison.
Sinan Ünür
You are being too critical. It is not wrong to use eval like that if it's not going to be used as real code; it seems to me that the OP is simply messing around.
Inshallah
@Inshalla Maybe ... maybe not. I am leaving the comment but got rid of my down vote.
Sinan Ünür
@Sinan Ünür: Leave it. I can see that you have made a good point.
Inshallah
@Inshalla: Tell you what for having the right answer, I'll give you my up, IF you put caveats in place.
Axeman
Rather than meta-discussion, let's state it straight: if you use eval, you're executing code from the config file. If the person who writes the config file is not supposed to have code execution rights, this is a Bad Thing. If you want to do it differently you would have to parse any expressions like this in your config file, and that's a lot harder than using 'eval'. And if you write the config file and the code yourself, then you have absolutely nothing to worry about.
ijw
I would probably add a disclaimer that this shouldn't be used in production code.
Brad Gilbert
+1  A: 

Here i a quick example that seems to satisfy your problem.

#! /usr/bin/env perl
use strict;
use warnings;

my @cond;
{
  while( <> ){
    chomp;
    next unless length;
    next if m' ^ \s* \# 'x;

    next unless m' (\w+) \s* = \s* (.*?) \s* $'x;
    push @cond, [$1,$2];
  }
}
my($a,$b);

$a=3;
$b=5;

for my $elem ( @cond ){
  my($name,$cond) = @$elem;

  if( eval $cond ){
    print "$name is true, because $cond matches "
  }else{
    print "$name is false, because $cond doesn't match "
  }
  print '(', eval("qq{$cond}"), ")\n";
}

echo 'condition1=$a>$b
condition2=$a<$b' | perl test.pl
condition1 is false, because $a>$b doesn't match (3>5)
condition2 is true, because $a<$b matches (3<5)
Brad Gilbert
If you wanted to make it safer you would match `(~~|[<>=]+)` instead of `(.*?)`
Brad Gilbert