Evaluating user-defined conditions

Jason Clifford jason at ukfsn.org
Mon Jun 9 12:03:13 BST 2014


On 2014-06-09 11:36, Andrew Beverley wrote:
> Dear all,
>
> I'd like to take a condition specified by a user and use it to 
> perform a
> set of tests on a data set. Is there a module to do this?
>
> For example, I might have an array of hashes containing "name", 
> "price"
> and "age". I would like a user to be able to define their own 
> condition,
> such as "age > 10 && price < (age + 5) * 10", and then to get the 
> result
> (true or false) for each of the array items.
>
> Is there a nice easy way to do this without trying to parse the
> condition myself? Presumably I could do some sort of eval, but that
> sounds highly dangerous with user input ;-)
>
> Andy

Take a look at Safe.pm for a way to do a safe eval.

We did something around this recently to provide something rather like 
what you describe and it works very well.



More information about the london.pm mailing list