note
ig
<p>I wonder if you are missing quotes in your second <c>if</c>.</p>
<p>I'm guessing that you also need to put quotes around your strings in what I'm guessing you want to be a string eval (the second <c>if</c>). The string you want to evaluate should probably be something like <c>"'string' eq 'string'"</c>.</p>
<p>It might help you to examine what you are evaluating. You might use the debugger but I would probably use print or warn myself. Something like:</p>
<c>
if (eval "$hash{$filter[0]->[0]} $filter[0]->[1] $filter[0]->[2]")
{
print "eval \"$hash{$filter[1]->[0]} $filter[1]->[1] $filter[1]->[2]\"";
if (eval "$hash{$filter[1]->[0]} $filter[1]->[1] $filter[1]->[2]")
{
if (eval "$hash{$filter[2]->[0]} $filter[2]->[1] $filter[2]->[2]")
{
print OUTFILE $line, "\n";
}
}
}
</c>
<p>When you see what you are evaluating, the problem will likely become evident.</p>
<p>FWIW: I don't think I would use <c>eval</c> as you are trying to. I would probably do something more like the follwing:</p>
<c>
sub match {
my ($hash, $filter) = @_;
if($filter->[1] eq '<=') {
return( $hash{$filter->[0]} <= $filter->[2] );
} elsif($filter->[1] eq 'eq') {
return( $hash{$filter->[0]} eq $filter->[2] );
} else {
die "unsupported comparator: $filter->[$index]->[1]";
}
}
if(
match($hash, $filters->[0]) and
match($hash, $filters->[1]) and
match($hash, $filters->[2])
) {
print OUTFILE $line, "\n";
}
</c>
<p>If I was going to use eval, I would definitely check for errors after each eval. I suspect if you did so, you would find that the eval in your second <c>if</c> gives a compile error - but it's only a guess. If you did add error handling to your implementation with <c>eval</c>, I would find it even harder to read than what you have currently, which is already too hard for my taste, but that's just me. In any case, if you want to know why your program isn't doing what you expect, a very good place to start is error handling and reporting. To this end, you might find [doc://strict] and [doc://warnings] helpful.</p>
982690
982690