$str = '( (1 == 1) and (2 < 1) ) or (2 > 0)'; while ($str =~ s/(\([^()]+\))/$1/ee) { if (not eval($1)) { print "error: $1\n"; last; } } #### $str = '( (1 == 1) and (2 < 1) ) or (2 > 0)'; while ($str =~ s/(\([^()]+\))/qq($1 || 0)/ee) { print eval($1) ? 'ok' : 'error'; print ": $1\n"; } #### ok: (1 == 1) error: (2 < 1) error: ( 1 and 0 ) ok: (2 > 0) #### $str = '( (1 == 1) or (2 < 1) ) and (2 > 0)';