In interpreted languages like Lua, PHP and Ruby, "require" is a statement that tells the interpreter to include a certain source-file at that position where the "require" statement has been placed.
Require in PHP
In PHP there exist the statements include()
and require()
to include another file with PHP-code into your document.
The difference to include is, that require will throw an error if the file is not available.
A variant of require()
is require_once()
. This includes a file only, if it hasn't already been included before.
require('/path/to/script.php');
Require in Ruby
In Ruby, the require
method does what include
does in most other programming languages: It will include and run another file.
It also tracks what you've required in the past and won't require the same file twice. To run another file without this added functionality, you can use the load
method.
require '/path/to/script.rb'
The include and require methods do very different things. Please check the source for further information on include.
Source: http://ruby.about.com/b/2008/10/23/a-quick-peek-at-ruby-include-vs-require.htm
Require in JavaScript
JavaScript has no native support for the require statement. However, there exist various implementations that try to mimic the behaviour of other programming languages.
These implementations are not fixed to use the word "require" for their loading routines, but may use other statements.
Some of them are:
const someModule = require('/path/to/module.js');
require(["moduleA", "moduleB"], function(A, B) {
// do something with moduleA ...
A.doSomething();
// do something with moduleB ...
B.doSomething();
});
head.js("/path/to/script.js");
$LAB.script("/path/to/script.js");
Require in Lua
The require statement of Lua searches for a module with the given name automatically extended by a previously defined path pattern.
The path patterns are rules that specify how to construct a path name using the parameter given to require
. After path construction, require will check if one of the constructed paths is valid in order to load this path.
The pattern usually includes an expression, which extends the given name by '.lua', Lua caches already loaded modules, so they won't be loaded twice.
require "module"