Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between use and require

Tags:

clojure

People also ask

What is require in Perl?

Description. This function then it demands that the script requires the specified version of Perl in order to continue if EXPR is numeric. If EXPR or $_ are not numeric, it assumes that the name is the name of a library file to be included.

What is use Perl?

In Perl, the use keyword is exactly equivalent to the following: use Mymodule; #is the same as BEGIN { require Mymodule; Mymodule->import(); } So if you are not defining an import routine in your code (or inheriting from Exporter ), then your modules are not importing anything into test.pl.

Where is @INC in Perl?

Perl interpreter is compiled with a specific @INC default value. To find out this value, run env -i perl -V command ( env -i ignores the PERL5LIB environmental variable - see #2) and in the output you will see something like this: $ env -i perl -V ... @INC: /usr/lib/perl5/site_perl/5.18.


require loads libs (that aren't already loaded), use does the same plus it refers to their namespaces with clojure.core/refer (so you also get the possibility of using :exclude etc like with clojure.core/refer). Both are recommended for use in ns rather than directly.


It's idiomatic to include external functions with require and refer. You avoid namespace conflicts, you only include functions you actually use/need, and you explicitly declare each function's location:

(ns project.core
    (:require [ring.middleware.reload :refer [wrap-reload]]))

I do not have to invoke this function by prefixing it with its namespace:

(wrap-reload) ; works

If you don't use refer you'll need to prefix it with the namespace:

(ring.middleware.reload/wrap-reload) ; works if you don't use refer in your require

If you choose use instead, (pretty much) always use only:

(ns project.core
    (:use [ring.middleware.reload :only [wrap-reload]]))

Otherwise you're including everything, making it both an unnecessarily large operation and very confusing for other programmers to find where the functions live.

Also, I highly recommend this blog as a resource for learning more about Clojure namespaces.


Use sure does make it easier by not requiring you to spell out the namespace every time you want to call a function though it can also make a mess of things by creating namespace conflicts. A good middle ground between "use" and "require" is to only 'use' the functions from a namespace that you actually use.

for instance:

 (use '[clojure-contrib.duck-streams :only (writer reader)])
or even better, specify it at the top of the file in the namespace definition:
(ns com.me.project
   (:use [clojure.contrib.test-is :only (deftest is run-tests)]))

As has been mentioned the big difference is that with (require 'foo), you then refer to names in the lib's namespace like so: (foo/bar ...) if you do (use 'foo) then they are now in your current namespace (whatever that may be and provided there are no conflicts) and you can call them like (bar ...).