Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should a Rebol-structured data file (which contains no code) be written and read?

If you build up a block structure, convert it to a string with MOLD, and write it to a file like this:

>> write %datafile.dat mold [
    [{Release} 12-Dec-2012]
    [{Conference} [12-Jul-2013 .. 14-Jul-2013]]
]

You can LOAD it back in later. But what about headers? If a file contains code, it is supposed to start with a header like:

rebol [
    title: "Local Area Defringer"
    date: 1-Jun-1957
    file: %defringe.r
    purpose: {
        Stabilize the wide area ignition transcriber
        using a double ganged defringing algorithm.
    }
]

If you are just writing out data and reading it back in, are you expected to have a rebol [] header, and extend it with any properties you want to add? Should you come up with your own myformat [] header concept with your own properties?

Also, given that LOAD does binding, does it make sense to use it for data or is there a different operation?

like image 264
HostileFork says dont trust SE Avatar asked Nov 24 '25 05:11

HostileFork says dont trust SE


2 Answers

Rebol data doesn't have to have a header, but is best practice to include one (even if it's just data).

Some notes:

  • SAVE is your best bet for serializing to file! or port! and has a mechanism for including a header.

  • MOLD and SAVE both have an /ALL refinement that corresponds to LOAD (without /ALL, some data from MOLD and SAVE cannot be reliably recovered, including Object, Logic and None values).

  • LOAD discards the header, though you can load it using the /HEADER refinement.

Putting this together:

save/all/header %datafile.dat reduce [next "some" 'data][
    title: "Some Data"
]

header: take data: load/header %datafile.dat

To use a header other than Rebol [], you'd need to devise a separate loader/saver.

like image 177
rgchris Avatar answered Nov 28 '25 03:11

rgchris


For the case of reading, construct works very well alongside load to prevent evaluation (of code as opposed to data):

prefs: construct/with load %options.reb default-prefs

It is:

Similar to context

obj: [
    name: "Fred" 
    age: 27 
    city: "Ukiah"
]

obj-context: context obj
obj-construct: construct obj

In this case, the same:

>> obj-context = obj-construct
== true

Different

when it comes to evaluating code:

obj-eval: [
    name: uppercase "Fred" 
    age: 20 + 7 
    time: now/time
]

obj-eval-context: context obj-eval
obj-eval-construct: construct obj-eval

This time parsing differently:

>> obj-eval-context = obj-eval-construct
false

>> ?? obj-eval-construct
obj-eval-construct: make object! [
    name: 'uppercase
    age: 20
    time: now/time
]

Aside:

This is the point I realize the following code wasn't behaving as I expected:

obj-eval: [
    title: uppercase "Fred" 
    age: 20 + 7
    city: "Ukiah"
    time: now/time
]

gives in red (and by extension, rebol2):

>> obj-eval-construct: construct obj-eval
== make object! [
    title: 'uppercase
    age: 20
    city: "Ukiah"
    time: now/time
]

lit-word! and lit-path! is different. TODO: question


It has also

Useful refinement /with

Which can be used for defaults, similar to make

like image 26
Geeky I Avatar answered Nov 28 '25 02:11

Geeky I



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!