I would like to read the entire string into a list, but not evaluate it. wtf? is the function or mix of functions which I can not find to do this.
user=> (wtf? " S I I ( S I I)")
(S I I (S I I))
The function should also work as:
user=> (last (wtf? " S I I ( S I I)"))
(S I I)
read-string returns only the first object, while load-string returns them all, but tries to evaluate them.
This is what I use:
(defn safe-read
"Evaluate the string in a safe way"
[s]
(binding [*read-eval* false]
(read-string s)))
From the doc:
"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))\"))
So what it does is read the string as usual, but disable the evaluation.
Thus you can use this function to read printed out maps, lists and vectors, without the fear to evaluate malicious code. (Ok I'm sure to what extend this is safe, but for day-to-day usage it does the job).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With