tags:

views:

368

answers:

9

It seems to me that eval() is treated with the same disdain that goto is. And by eval, I mean a function for executing a string as code, as seen in PHP, Python, JavaScript, etc.. Is there ever a situation where using eval() is justified(except perl)? And if not, why do so many languages implement it?

+2  A: 

For quick hacks, no problem because it's a handy quick-out.

In production code, consider it a last resort—and even then, try something else—because eval is difficult to control and thus dangerous. For anything non-trivial, implement a sublanguage.

Greg Bacon
A: 

Writing a cool textbook example on how easy it is to implement a "calculator" in language X? =)

Yacoder
+1  A: 

Offhand thought: eval is good for implementing a poor man's expression compiler, or things like that. It's also a dull, rusty substitute for hygienic macros.

Jonathan Feinberg
A: 

I used it once while pentesting a site - we wrote a small php script that decrypts and executes cryptographically signed payloads from non-logged HTTP data sources on the fly. This is the best use I've seen of eval() so far.

(In other words: no, I've never seen a good use for eval)

Longpoke
+1  A: 

Maybe I use sh and perl too much, but I've never seen anyone treat eval with the disdain that goto gets.

So my answer is: 'eval is suitable when you are writing perl 5 and sh'. The block eval is the primary try/catch mechanism in Perl and its hard to write safe code without it.

William Pursell
GSto
corollary: don't tag anything as 'language-agnostic' unless you're comfortable with at least a dozen languages
Javier
+3  A: 

Yes - when there is no other way to accomplish the given task with a reasonable level of clarity and within a reasonable number of lines of code.

This eliminates 99% of cases where eval is used, across the board in all languages and contexts.

Justice
+2  A: 

eval is often the most expedient solution in situations where you are dynamically generating code. Even in languages that do not officially support eval, such as Java, they support reflection and modification of classes at runtime which are similar. (See books such as Stu Halloway's Component Development for the Java Platform )

MattMcKnight
+1, only real good example I've seen so far.
e-satis
A: 

One reasonable use is if you have an interpreted language that you've built on top of another language, but you still want to provide some sort of "escape hatch" to allow people to get back to functions that are provided by the underlying language. One example is implementing Prolog in Lisp and then defining a predicate that allows direct use of Lisp functions via EVAL.

Pillsy
A: 

For debugging/testing an idea before implementing it the proper way.

For instance, you're making a toy calculator, and you want to work on the gui first, so you just use eval to do the "back-end" work in the background. Later, you come back to the back-end, scratch eval, and write a proper expression parser.

hasen j