in reply to Using dynamic operators in an if() statement
if (eval "\$consumption $operator")
But I wouldn't recommend you do that. It means any errors in $operator will be runtime instead of compile-time errors, and that code will be re-compiled every time through your loop, which is bad for performance.
Instead, consider customizing your condition with a small sub, something like:
This gives you the same flexibility without any of the downsides of string eval.sub process_match { my($consumption)=@_; return $consumption > 98.0; } if (process_match($consumption)) { do something; }
Good luck!
Update: $consumption should be escaped in eval. Thanks, ikegami!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Using dynamic operators in an if() statement
by VSarkiss (Monsignor) on Aug 22, 2006 at 15:06 UTC |