(Addressing the update)
You are litterally trying to execute the code if($port_a $cmp_op $port_B) and that isn't valid perl syntax. A quick fix would probably be to just define $port_a,$cmp_op and $port_b before you initialize $code and then just do $code = "if($port_a $cmp_op $port_b)" which will work.