Like 'require, but also refers to each lib's namespace using clojure.core/refer. Use :use in the ns macro in preference to calling this directly. 'use accepts additional options in libspecs: :exclude, :only, :rename. The arguments and semantics for :exclude, :only, and :rename are the same as those documented for clojure.core/refer.
;; Imports only the split function from clojure.string. user=> (use '[clojure.string :only (split)]) nil ;; split is now available without a namespace qualification. user=> (split "hello world" #" ") ["hello" "world"] ;; You can also add the :as keyword to import the rest of clojure.string ;; with a namespace qualification. user=> (use '[clojure.string :as s :only (split)]) nil ;; Now we can access any function in clojure.string using s. user=> (s/replace "foobar" "foo" "squirrel") "squirrelbar" ;; And we can still call split with or without the s qualification. user=> (split "hello world" #" ") ["hello" "world"] user=> (s/split "hello world" #" ") ["hello" "world"]
(ns some.namespace (:require [clojure.contrib.json :as json]) (:use [clojure.string :only [trim lower-case split]] [clojure.contrib.shell-out] [clojure.pprint] [clojure.test]))
;; use accepts other keywords from require that aren't listed in the docstring. ;; If you try to load a namespace, and it fails to load due to an error in ;; the source code, when you load it again after fixing the problem, you ;; can get a "namespace not found" exception. Use :reload to avoid this: (use '[my.namespace] :reload) ;; However, if the error was in source for a namespace required or used ;; from my.namespace, you'll get the "namespace not found" exception ;; after fixing the problem, even using :reload. Use :reload-all to avoid this: (use '[my.namespace] :reload-all) ;; You can also use :verbose, which does what you would think it would do: (use '[my.namespace] :verbose)
;; As the docstring says, ;; ;; The arguments and semantics for :exclude, :only, and :rename are the same ;; as those documented for clojure.core/refer ;; ;; but the syntax for these options is derived from require, not refer. ;; Here is an example using Clojure 1.11, in which the abs function was ;; introduced into clojure.core. ;; First let's try it with require and refer separately: user=> (require 'clojure.math.numeric-tower) nil user=> (refer 'clojure.math.numeric-tower :exclude '[abs]) nil user=> abs #object[clojure.core$abs 0x36fdf831 "clojure.core$abs@36fdf831"] ;; The last line shows that we've correctly excluded the version of ;; abs that numeric-tower defines, and instead are using the version of abs ;; that's now in clojure.core. ;; Now let's try it with use. The refer-style syntax doesn't work: user=> (use 'clojure.math.numeric-tower :exclude '[abs]) Syntax error compiling at (/private/var/folders/68/d0l7z7p906l07fj6s7j5_ygm0000gq/T/form-init6849217183418449171.clj:1:1). Unsupported option(s) supplied: :exclude ;; Instead :exclude must be wrapped up with the namespace in a sequence, ;; as with options for require: user=> (use '[clojure.math.numeric-tower :exclude [abs]]) nil user=> abs #object[clojure.core$abs 0x13abd924 "clojure.core$abs@13abd924"]
refers to all public vars of ns, subject to filters. filters can include at most one each of: :...
Loads libs, skipping any that are already loaded. Each argument is either a libspec that identifie...
Sets *ns* to the namespace named by name (unevaluated), creating it if needed. references can be ...
import-list => (package-symbol class-name-symbols*) For each name in class-name-symbols, adds a m...
Good description of use/require/import here:
http://blog.8thlight.com/articles/2010/12/6/clojure-libs-and-namespaces-require-use-import-and-ns
The blog post from 8thlight is not available under the above URL anymore. The Wayback Machine has a snapshot of it.