perl - parsing conditional operators as strings and using as conditional operators -
my $line ="corner:default,output:fall_delay_slew_1,mean=34.97p,std- dev=1.767p,min=30.02p,max=39.71p"; #added semicolon $my_value="cond = mean > 3"; #this has come parsed file. $my_value =~ m/(\w+)\s*(.)\s*(\d+)/; $cond=$1; $sign=$2; $value=$3; print "debug:cond $cond , sign $sign , value $value \n"; if ( $line =~ m/$cond=(.*?),/) { if ( "$value $sign $1" ) { print "$value $sign $1\n"; } else { print "actual value less\n"; } }
if see in above if statement evaluates true.
how can solve kind of problem i.e $sign = "<"
(could operator) when want compare $value
want function operator , not string.
what you're willing (executing string code) can done eval
. doesn't mean appropriate way of doing though. if guarantee input safety , check it.
a better way checking operator self , determining how proceed. if use recent perl version, given-when
feature can handy this:
use feature 'switch'; # not needed if use 5.010 or greater given ($sign) { when ('<') { "$cond less $value" } when ('>') { "$cond greater $value" } default { warn "unrecognized operator `$sign'\n"; # decide } }
Comments
Post a Comment