Script or object... ?
[EMAIL PROTECTED] wrote:
>
> Does anyone know whether Rebol has such a kind of variable: you
> initialize this variable at the first time you run the script.
> In this script, the variable will be modified to some value. If
> you run this script again, this variable will preserve the value
> it has in last run.
>
There are several ways to look at this.
1) If you want persistence of private data between evaluationss
of a function (without reloading the function from a file)
then the technique posted by mjelinek will do that.
2) If you want persistence of data (independent of function
associations, and without reloading from a file) then the
technique posted by doug.vos will do that.
3) If you want persistence of state for some "concept" (without
reloading from a file, and with the option of multiple
functions being privy to that state) then you've just stumbled
over the canonical rationale for object-oriented programming.
For example:
REBOL []
statefulthing: make object! [
val: 0
set: func [n [integer!]] [val: n]
get: func [] [val]
inc: func [] [val: val + 1]
dec: func [] [val: val - 1]
]
used as in
>> statefulthing/get
== 0
>> statefulthing/inc
== 1
>> statefulthing/inc
== 2
>> statefulthing/get
== 2
>> statefulthing/set 27
== 27
>> statefulthing/dec
== 26
and so on.
4) If you like the tidyness of the above, but want persistence
between REBOL sessions, you can do the following:
>> statefulthing/get
== 26
(Just to show current state...)
>> save/header %statefulthing.r statefulthing []
Now the state is safely stashed in a file.
>> statefulthing: "simulate stopping and restarting REBOL"
== "simulate stopping and restarting REBOL"
>> statefulthing: none
== none
(Just to show that the state is no longer "in" that word.)
>> statefulthing: do %statefulthing.r
Get it back!
>> statefulthing/inc
== 27
5) If you want persistence of data and you really do mean "script"
and you really do want to reload the script from a file between
usages (and don't mind getting REALLY obscure!) you can do this:
REBOL []
staticvar: none
somefunction: func [s [string!]] [
either found? staticvar [
print rejoin [{staticvar was "} staticvar {"}]
][
print {staticvar had no value}
]
staticvar: s
print rejoin [{staticvar is now "} staticvar {"}]
write %static.r {REBOL []^/^/}
write/append %static.r
rejoin [{staticvar: "} staticvar {"^/^/}]
write/append %static.r
rejoin [{somefunction: } mold get 'somefunction {^/}]
]
(in a file named static.r, of course!) Now use it as ...
>> do %static.r
>> somefunction "Hello"
staticvar had no value
staticvar is now "Hello"
>> somefunction "world!"
staticvar was "Hello"
staticvar is now "world!"
>> somefunction "still in memory"
staticvar was "world!"
staticvar is now "still in memory"
In-memory persistence is accomplished, as staticvar is global,
but the use of somefunction is also keeping the external (file)
copy synched, so that
>> somefunction: none
== none
>> staticvar: none
== none
(which simulates shutting down and restarting REBOL) can be
followed by
>> do %static.r
>> somefunction "I'm back!"
staticvar was "still in memory"
staticvar is now "I'm back!"
>>
As me auld granny sed, "There's more than one way to skin a cat!"
-jn-