Loading Configuration Data

I’m always pleased at how REBOL allows you to skip work that you’d normally have to do in other languages, such as loading configuration data and making use of this data in your programs.

Normally, you might have to:

- Invent a configuration file format.
- Decide on a method to store configuration data in a file.
- Decide on a method to load and parse configuration data and use it in the program.

or if you are a former XML fan, you’d find a bigarse library to parse XML files, written out with an XML generator. Bloated. Slow.

If you like a challenge, you can do all that in REBOL. REBOL has a fine parser, that will easily parse strings of text read from disk.

But REBOL can do all of this without you writing a lot of boring code.

The magic trick to use here is LOAD. LOAD is pretty good at loading data and you can use LOAD on files, internal data and network resources.

If you create a file, say, %config.r, which contains:

name: "My Name"
address: "My Address"
registration-number: 123456789

and nothing else, you can LOAD this file into a block in one go:

>> load %config.r
== [name: "My Name" address: "My Address"...

Now what REBOL does here is more than just reading the data off disk; It creates a block of valid REBOL elements of the correct datatypes. You now actually have a valid piece of data that is ready to be further processed or run. Yes, run!

Because when you DO the loaded data, the data is suddenly code that is executed. The items "name:" "address:" and "registration-number:" become variables in the system, with their respective values assigned to them.

The full piece of code would be:

do load %config.r

This is actually all you need to do! Ridiculously simple!

You can do this, because data and code are the same thing in REBOL. Imagine the possibilities... Happy
|