REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 26-Apr-2010 Edit History |
There are a few ways to load modules:
needs | is a block that you can put in your program header to specify the modules your program wants loaded to run. |
import | called in your code to load a module from a file, URL, or name. |
do | called in your code with a module file or URL. |
module | directly creates modules from blocks that contain a module definition. |
make | lower level module creation function. Takes a block that contains a module definition. |
If your program requires one or more modules, you can specify them in your program's header block using the needs field.
For example:
REBOL [ Title: "Super Web Site" Author: "John Adams" Needs: [mysql http-server] ] db: open-db %my-database.sql ...
When this file is loaded, the header is examined, the mysql and http-server modules are imported, then the main body of the program is evaluated.
Once a module has been imported, all of it's exported functions and other values are available to be used by your code. In the example above, the open-db function is defined by the mysql module.
A few types of module identifiers are allowed, and are summarized in this table:
Datatype | What | Action |
---|---|---|
word! | module by name | search multiple locations for the module |
file! | local file path of module | must be found at that location |
url! | Internet location of module | must be found at that location |
When the module name is a word, REBOL will search for the module in a few locations.
Here's what happens:
Here is an example that uses all three types of module identifiers:
Needs: [ mysql %mods/database.r http://www.rebol.com/mods/http-server.r ]
In this example:
If your program requires specific module versions, they can be specified as well:
Needs: [ mysql 2.3.0 http-server 1.0.1 ]
For the mysql module a version of 2.3.0 or greater is required. For http-server the version must be 1.0.1 or greater.
Also note that if a version number appears before any module name, it is assumed to be the REBOL system version. So:
Needs: [3.0.2 mysql 2.3.0 http-server 1.0.1]
indicates that the script needs REBOL 3.0.2. If the REBOL version is less, an error message will be displayed.
In addition to specifying a module name and version, you can also specify that modules be validated using a hash checksum.
This is a type of integrity check. It is done to verify that the module contents have not been modified in any way (but, read the note below, because it's not perfect.)
Needs: [ mysql 1.2.3 #{A94A8FE5CCB19BA61C4C0873D391E987982FBBD3} http-server 1.0.1 #{8E67416A43298507CC22135E28233F4B9C27E75D} ]
Here the two modules will be located and read in to memory. The hash checksums will be computed for their source code (not including the header) and compared to the header. If they are the same, the load continues. If not, an error is displayed.
You can also import modules directly using the import function. This function follows the same rules as the needs field of a program header, as explained above.
For example, you can write:
import 'mysql
and the system will search for the mysql module, using the method described above.
You can also use a filename or URL for the module identifier:
import %mysql.r import http://www.rebol.com/mods/mysql.r
And, you can import from a block of modules (which is in fact a valid needs block):
import [mysql db-gui http-server]
When successful, the import function returns a module! datatype as its result.
This allows you to write:
mysql: import 'mysql
Now, the mysql variable can be used to refer to values within the mysql module.
For example the module value is used here to reference a function:
mysql/open-db %my-database.sql
See below for more.
Like the header needs field, the import function also lets you specify a version and a checksum.
These are all supported:
import/version mysql 1.2.3 import/check mysql #{A94A8FE5CCB19BA61C4C0873D391E987982FBBD3} import/version/check mysql 1.2.3 #{A94A8FE5CCB19BA61C4C0873D391E987982FBBD3}
The benefit of using the import function compared to the needs header field is that the arguments can be variables.
A basic example is:
mod: 'mysql import mod
Or, something like:
mod-list: [ mysql 1.2.3 db-gui 2.4.5 http-server 1.0.1 ] import mod-list
Another method of importing modules is supported. If you use do on a module file, it will also be imported. This is provided as a convenience, because do has been used in REBOL for years as a way to load and initialize additional functions and values.
For example, writing:
do %mysql.r do http://www.rebol.com/mods/mysql.r
is equivalent to:
import %mysql.r import http://www.rebol.com/mods/mysql.r
But note that:
import 'mysql
is not the same as:
do 'mysql
because do of a word simply returns the value word.
Note also that do does not support the version and checksum refinements found in the import function.
What happens when an imported module uses the same global variable name as one you define in your program?
Here is an example. A program loads a module that exports the function select-data:
import %database.r
But, the program defines it's own function:
select-data: func [database selector] ...
Both of the functions can't use the same name as global variables.
This situation can be solved by using the result of the import function as a way to reference its specific values.
Here's an example:
db-mod: import %database.r ; Define our own local function: select-data: func [database selector] ... ; To use the module's function: db-mod/select-data ...
This is identical to how you reference a field of an object.
Modules are also allowed to be created dynamically by a program. This is normally done for two reasons:
For more about these types of modules, see modules: defining modules.
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |