REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 11-Aug-2010 Edit History  

REBOL 3 Functions: do

do  value  /args  arg  /next  var

Evaluates a block, file, URL, function, word, or any other value.

Arguments:

value [any-type!] - Normally a file name, URL, or block

Refinements:

/args - If value is a script, this will set its system/script/args

arg - Args passed to a script (normally a string)

/next - Do next expression only, return it, update block variable

var [word!] - Variable updated with new block position

See also:

reduce   load   import   loop   repeat   call   launch  

Description

The do function evaluates a script file or a series of expressions and returns a result.

It performs the fundamental interpretive action of the REBOL language and is used internally within many other functions such as if, case, while, loop, repeat, foreach, and others.

Most Common Use

Most of the time do is used to evaluate a script from a file! or url! as shown in these examples:

do %setup.r
Settings done.
do http://www.rebol.com/speed.r
Console:   0:00:01.609 - 314 KC/S
Processor: 0:00:00.406 - 2128 RHz (REBOL-Hertz)
Memory:    0:00:00.657 - 72 MB/S
Disk/File: 0:00:00.234 - 130 MB/S

Note that do of a file! or url! requires that the script contain a valid REBOL header; otherwise, you'll get an "Script is missing a REBOL header" error.

Warning

Only do a url! script that you have reason to trust. It is advised that you read a script first and examine it closely to make sure it is safe to evaluate.

Other Uses

The do function can also be called to evaluate other types of arguments such as a block!, path!, string!, or function!.

do [1 + 2]
3
do "1 + 2"  ; see special note below
3

Expressions are evaluated left to right and the final result is returned. For example:

do [1 + 2 3 * 4]
12

To obtain all results, use the reduce function instead.

print reduce [1 + 2 3 * 4]
3 12

Other Examples

Selecting a block to evaluate:

blk: [
    [print "test"]
    [loop 3 [print "loop"]]
]
do first blk
test
do second blk
loop
loop
loop

Refinements

The /args refinement allows you to pass arguments to another script and is used with a file, or URL. Arguments passed with /args are stored in system/script/args within the context of the loaded script.

The /next refinement returns a block consisting of two elements. The first element is the evaluated return of the first expression encountered. The second element is the original block with the current index placed after the last evaluated expression.

Special Notes

Evaluating strings is much slower than evaluating blocks and values. That's because REBOL is a symbolic language, not a string language. It is considered bad practice to convert values to strings and join them together to pass to do for evaluation. This can be done directly without strings.

For example, writing code like this is a poor practice:

str: "1234 + "
code: join str "10"
do code
1244

Instead, just use:

blk: [1234 +]
code: join blk 10
do code
1244

In other words, you can join values in blocks just as easily as strings.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin