ClojureDocs

Nav

Namespaces

pr

clojure.core

Available since 1.0 (source)
  • (pr)
  • (pr x)
  • (pr x & more)
Prints the object(s) to the output stream that is the current value
of *out*.  Prints the object(s), separated by spaces if there is
more than one.  By default, pr and prn print in a way that objects
can be read by the reader
4 Examples
user=> (pr "foo")
"foo"nil
user=> (pr {:foo "hello" :bar 34.5})
{:foo "hello", :bar 34.5}nil
;; Difference between pr and print

user=> (pr ['a :b "\n" \space "c"])
[a :b "\n" \space "c"]nil

user=> (print ['a :b "\n" \space "c"])
[a :b
   c]nil
;; WARNING: Clojure's keyword and symbol functions allow you to create
;; values that cannot be printed then later read and recreate those same
;; values.

;; For most common symbols and keywords, they round-trip through pr
;; then read-string, as this example shows.
user=> (read-string (with-out-str (pr {:a 1, :b 2})))
{:a 1, :b 2}

;; But these values do not round-trip through the same steps:
user=> (def kw1 (keyword "foo bar"))
#'user/kw1
user=> kw1
:foo bar
user=> (def s2 (with-out-str (pr {kw1 1, :b 2})))
#'user/s2
user=> s2
"{:foo bar 1, :b 2}"
user=> (read-string s2)

RuntimeException Map literal must contain an even number of forms  clojure.lang.Util.runtimeException (Util.java:221)

;; Similarly for a symbol like (symbol "a;b")
user=> (def sym1 (symbol "a;b"))
#'user/sym1
user=> sym1
a;b
user=> (def s3 (with-out-str (pr sym1)))
#'user/s3
user=> s3
"a;b"
user=> (read-string s3)
a

;; If you wish to transmit data that may contain such values, one suggestion
;; is to use the transit library: https://github.com/cognitect/transit-format

;; It is much faster and formally specified.
See Also

Prints the object(s) to the output stream that is the current value of *out*. print and println p...

Added by dale

Same as pr followed by (newline). Observes *flush-on-newline*

Added by TimMc

pr to a string, returning it

Added by TimMc

Reads the next object from stream, which must be an instance of java.io.PushbackReader or some der...

Added by jafingerhut
1 Note
    By , created 7.3 years ago

    "Readable by the reader" means, for example, that a string is printed with surrounding double quotes and with quotes, backslashes, and nonprinting characters being escaped via a backslash prefix.