The magazine of the Melbourne PC User Group

REBOL An Overview
Trevor Gosbell

 


Trevor Gosbell presents the first instalment of a series on this versatile, easy to learn and use programming language which also runs on Linux.



REBOL. I first heard of this new programming language several years ago. The name sent shudders down my spine - was it the "REturn of coBOL"? Visions of endless pages of data declarations crawled out of a dark place in my memory and I moved on to another Web site while I still had the will to live.

What a shame - I should have had a closer look, because I couldn't have been more wrong. I don't know if REBOL is ever going to set the programming world on fire, but it is easy to learn, surprisingly powerful and a lot of fun to code. The purists insist it is pronounced "reb-el", but I just can't seem to get past "re-bol".

What Is it?

The inventor of REBOL (Relative Expression-Based Object Language) refers to it as a messaging language - "created specifically for the exchange and interpretation of information over the Internet". One of the major implications of this is that REBOL treats local and remote data sources identically - it doesn't matter to the REBOL programmer whether files are on the local hard drive, on a Web site or FTP server. In practice REBOL sits in roughly the same space as the scripting languages like Perl, Python, VB, and Java.

Multi-tier Structure

REBOL has a multi-tier structure. At the base is REBOL/Core, the command line kernel that provides the basic functionality of the language (common to all other modules). On top of this sits REBOL/View (providing GUI and multimedia features) and REBOL/Command (adding "enterprise" functionality like access to databases, external libraries and external applications). At the top of the tree is REBOL/IOS, the Internet Operating System - "a platform for collaborative applications".

Availability

REBOL/Core and REBOL/View are freely available but are not open source. Both are a relatively small download at about 250 KB and 360 KB respectively. REBOL/Command and REBOL/IOS are fully commercial products.

Language Features

Here is a brief overview of the features provided to all other packages by the REBOL/Core module.

Platform Independence

REBOL/Core is intended to be platform independent, as is REBOL/View on systems where a GUI environment is available. I have found this to be a reasonable claim after running REBOL/Core and REBOL/View on Windows, Linux and Macintosh OS9 and OSX (except for REBOL/View on OSX - it's not available at the time of writing). Both versions are available for a wide range of platforms, from Amiga to Windows.

There are some limitations imposed by strict platform independence, one of which is the inability for the free versions to execute external applications. The REBOL folks claim this is to preserve cross-platform compatibility but it is a feature that the language really needs in order to replace other scripting languages on the desktop. Conversely, it is possible to access command line arguments but not all operating systems have the concept of a command line - an odd little inconsistency.

Installation

There is practically no installation required - just unpack the zip archive and run REBOL. If you want to use Network features there is some configuration required, but a comprehensive setup page on the REBOL Web site http://www.rebol.com/docs/setup.html should see you right. REBOL/View comes with an installer that steps you through Network setup. If you want to call REBOL like any other program, it will need to be in your system path.

Values

In REBOL, raw data are referred to as values, and each value has a data type. Like most other programming languages REBOL provides the common native data types like numbers, characters, strings, and so on. But it also includes many others such as URLs, e-mail addresses, SGML-style tags, and files, all as standard data types in the language.

Words

Words are the labels used by REBOL. Words may be variable names, function names, or just ... words!

Variable Names
 
my-name: "Trevor"
pay-me-now: $1000000.00

Function Names

add-up: func [this that] [this + that]
factorial: func [x] [either x > 1 [x * factorial (x - 1)] [1]]

Literal Words

'blue
'something

Blocks

Words, making up both program code and data, are collected into blocks. Blocks are delimited by square brackets, and can be assigned to a variable name just like any other value.

For example, the following works just as well as the previous definition of the "add-up" function:

params: [this that]
procedure: [this + that]
add-up: func params procedure
 

Blocks are the main structural feature of REBOL scripts.

Network Protocols

A useful collection of Network protocols is seamlessly integrated into the language, including HTTP, FTP, SMTP, POP and TCP. A good demonstration of the simplicity of REBOL's Networking capability is this one-line e-mail sender:

send someone@email-address.com
"This is the message I am sending."

This prints out the HTML source of the given Web page:

print read http://www.melbpc.org.au

Dialects

One of the most exciting features of the language is the ability to virtually rewrite the language using "relative expressions" or "dialects". This can help simplify otherwise complex tasks. Here is an example using the Visual Interface Dialect (VID) that is resident in REBOL/View:

view layout [image http://www.melbpc.org.au/pict/mpclogox.gif]

It puts the Melb PC logo in a window on your screen.

I will return to REBOL/View in a later article. Third party programmers have also written other dialects including ones for HTML, XML and Flash.

Evaluation Of Expressions

There are no operator precedence rules in REBOL - all processing proceeds left-to-right. For example:

10 + 20 * 3

gives the result 90 not 70 as primary school mathematics dictates. Use brackets or reorder the operators to make the intention clear. This does not present as much of a problem as you would think at first.

An example Script - A Batch File Renamer

All of the previous code samples in this article can be tried at the REBOL command line. Now I present a short but complete REBOL script (see Listing 1 below).

Firstly every REBOL script has a header, enclosed in the square brackets [ ], that includes some descriptive information, followed by the code of the script itself. Comments are embedded in the code, preceded by a semi-colon.

Enter the script into a text editor (or download it from http://members.melbpc.org.au/~tgosbell/) and save it as rebname. This is a command line utility - at the command line usage is:

rebol rebname find-pattern replace-pattern

For example, I may have a set of documents called letter-draft.doc, budget- draft.xls, presentation-draft.ppt, and so on. To change the drafts to final versions, I can enter:

rebol rebname draft final

And the job is done.

In the next instalment, I will outline how to use REBOL/View to make a graphical front end for the renamer.

 REBOL [

  Title: "REBOL rename"

  Date: "March 2003"

  Name: "rename"

  Version: 0.1

  File: "rebname"

  Author: "Trevor Gosbell"

  Owner: "Trevor Gosbell"

  Rights: "Open to Melb PC members"

  Usage: {rebol rename "find-pattern" ["replace-pattern"]}

  Purpose: {For all file names in the current directory, replaces find-pattern 

with replace-pattern.

If replace-pattern is omitted, find-pattern is removed from the file names.}

  Comment: {It CHANGES FILE NAMES - use at your own risk!}

] 

; retrieve command line arguments from the system object and convert to a block

cl_args: make block! system/script/args 

; if the first argument exists, assign it to the search term

search_term: either none? cl_args/1 [""] [to-string cl_args/1] 

; if the second argument exists, assign it to the replace term

replace_term: either none? cl_args/2 [""] [to-string cl_args/2] 

; change working directory to start up directory (change-dir system/options/path)

; read all of the filenames in working directory (read)

; loop through all of the files in the directory (foreach file)

foreach file read change-dir system/options/path

[

  ; process files only, not directories

  if not dir? file

  [

    ; only proceed with this file if the search term is found

    if not none? find file search_term

    [

      ; make a new file name by replacing the search term with the replace term

      new_file: to-file replace to-string file search_term replace_term 

      ; if the filename already exists, don't rename the current file

      either exists? new_file

      [

        ; print a warning

        print ["FILENAME" new_file "ALREADY IN USE"]

      ]

      [ 

        ; print notification

        print ["Renaming" file "to" new_file] 

        ; rename the file (finally!)

        rename file new_file

      ]

    ]

  ]

] 

; exit the REBOL environment when finished

quit 

      

Listing 1. A Batch File Renamer  (This listing can be downloaded directly from this link.)

About the Author
Trevor Gosbell's first program - a rude variation on "Hello World" - was saved to paper tape in 1981. However it wasn't until the late 1990s that he began making a living from programming, as a programmer and project manager on Web database projects for a Sydney based software house. He is currently a Web site content manager at a well-known Melbourne educational institution. In his spare time he writes programs and writes about programming.

REBOL Resources on the Web

http://www.rebol.com The REBOL homepage has downloads including REBOL/Core, REBOL/View and the REBOL manual

http://www.reboltech.com/library/library.html The REBOL script library has over 400 sample scripts available

http://www.rebolforces.com/ Provides an online magazine about REBOL

http://www.codeconscious.com/rebol/ "Programming with REBOL" has plenty of good code samples, tip, techniques and tutorials

http://www.shlik.org/links.html An extensive collection of REBOL resources.

Reprinted from the May 2003 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[ About Melbourne PC User Group ]