in reply to Re^3: How to pass a variable string as conditional argument to if statment
in thread How to pass a variable string as conditional argument to if statment

Hi, I have a variable $var = '10>5"; This string is obtained from a table and is stored in the variable. I want to evaluate this string. If it is true, then execute. E.g, if($var is true){ }else { } I tried to use eval but is not working as expected.
  • Comment on Re^4: How to pass a variable string as conditional argument to if statment

Replies are listed 'Best First'.
Re^5: How to pass a variable string as conditional argument to if statment
by poj (Abbot) on Feb 06, 2016 at 07:00 UTC

    Are you checking for errors after the eval ?

    #!perl use strict; my @test = ('10>5','10<5','13+2==14+1','4>(6/2)+1'); for my $var (@test){ my $result = eval $var ? 'True' : 'False'; die $@ if $@; # check for errors in eval print "$var is $result\n"; }
    poj
Re^5: How to pass a variable string as conditional argument to if statment
by choroba (Cardinal) on Feb 06, 2016 at 08:25 UTC
    You should validate the string before running eval on it. Imagine the string contains
    system 'rm -rf /'

    or something similar.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,