On 2010 Apr 22, at 8:17 AM, Base wrote:
say i have a string that contains a form:

"(+ 1 1)"

I want to actually execute this.  How do you do this?  I thought that
eval would be able to handle this but apparently am misunderstanding
what eval does.

Well, eval is the second half of what you want.
eval evaluates a form.
user=> (read-string "(+ 1 1)")
(+ 1 1)
user=> (eval (read-string "(+ 1 1)"))
2

eval can be a dangerous thing to use, you have to be very careful about where the source has come from, in terms of trusting that the code your programs 'eval's will not be malicious or dangerous in some way. There are no absolute rules for this, it depends on your application.


read can also be a dangerous thing to use, as per the doc on *read- eval* quoted here:
clojure.core/*read-eval*
nil
When set to logical false, the EvalReader (#=(...)) is disabled in the
  read/load in the thread-local binding.
Example: (binding [*read-eval* false] (read-string "#=(eval (def x 3))"))

  Defaults to true

(Note to doc folks, #= does not seem to be described on: http://clojure.org/reader with the other # reader macros.)

-Doug

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to