[REBOL] Re: what to do with bind ?
From: greggirwin:mindspring at: 25-Nov-2001 15:04
Hi Patrick,
<< But I wonder if 'bind is only useful for those who explore the guts of
Rebol. I'm looking for an example that enlighten the use of bind in a simple
way. >>
I hope I won't confuse matters, being a relative beginner myself, but here's
how I recently used bind.
I created a basic AWK dialect and implemented it as an object. AWK provides
certain built-in variables that allow you to reference the current line,
fields in that line, the number of records read, etc. and they are designed
to have a very concise syntax (e.g. $1 = field 1 in the current line, $0 is
the current line in its entirety).
Since the goal is to write AWK "programs" outside the context of my object,
I needed a way to reference the built-in variables and I wanted to avoid the
requirement of fully qualifying every reference with the context name.
That's what 'bind allowed me to do. Here's the basic idea:
rawk: context [
_: none ; current parsed line/record
exec: func [
{Execute the RAWK 'program' contained in the prog block for each
file
contained in the files block.}
program [block!]
files [block!]
][
bind program 'self
; EXECUTE PROGRAM HERE
]
]
; If bind isn't used, we have to do this:
rawk-prog: [
[rawk/_/1 = "lng"] [print ["found line with _/1 = lng:" rawk/_]]
]
; If bind is used, we can do this:
rawk-prog: [
[_/1 = "lng"] [print ["found line with _/1 = lng:" _]]
]
HTH!
--Gregg