Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: How to pass a variable string as conditional argument to if statment

by Anonymous Monk
on Feb 05, 2016 at 20:32 UTC ( [id://1154512]=note: print w/replies, xml ) Need Help??


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.

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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1154512]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-28 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found