read-string parses a string containing a Clojure expression and returns its data-structure representation. Reading happens before macro expansion or evaluation, so the result is data rather than an immediately executed program. section 17.1 section 17.10
Use read-string when input is already held as a string:
- Pass the string as its input; read-string otherwise follows the options and behavior of read. section 17.1
- Receive data such as a list representing (+ 1 2), rather than the result 3. section 17.1
- Supply :eof to control the result when the input contains nothing to read. section 17.1
- Control reader evaluation and other reader features through the same mechanisms available to read. section 17.1
Parsing a form
(read-string "(+ 1 2)") ;; (+ 1 2) (read-string {:eof "nothing to read"} "") ;; "nothing to read" The returned form can be inspected, transformed, or passed separately to eval when execution is intended. section 17.1 section 17.10
(read-string "(+ 1 2)") ; #1
;; (+ 1 2)
(read-string {:eof "nothing to read"} "") ; #2
;; "nothing to read"
Browse executable examples showing ordinary parsing and customized empty-input behavior. section 17.1
