Quick question: eval and read-from-string scoping



Hi,

When I run this code in CLisp:

(let ((left 5) (top 10))
(eval(read-from-string "(eq left (margin top 20))")))

When executed, it complains that "*** - EVAL: variable left has no
value". At first I wondered why the local variables were not in scope for
the eval'd code and was going to post, but I found this thread:

http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/846c4f
53e91ef251/69239105712fed0d?lnk=st&q=lisp+eval+global+variables&rnum=2#
69239105712fed0d

And now I realise that eval is like a function, and local variables are
not passed from function to function. So the question is revised: How do
I pass variables to code executed by eval without declaring them as
special?

I realise that special variables can be re-defined using 'let' and they
are passed down to any functions called by the parent, but this doesn't
appear to be working when calling eval. Code run by eval cannot see the
re-bound special variables. Odd!

Finally, my reasons for knowing this is that I am implementing a small
description language for OCR (optical character recognition) software but
I'm implementing it in Lisp before I write a parser, so I 'eval' the
descriptions as if read in from a text file. The reason I need to be able
to pass data to the eval'd code is because they are the attributes of the
current character object (such as top,bottom,left,right) and thus should
not be global.

Thanks for any help,

Jeremy.
.



Relevant Pages

  • Re: Quick question: eval and read-from-string scoping
    ... it's a character string that only becomes ... > And now I realise that eval is like a function, and local variables are ... > I realise that special variables can be re-defined using 'let' and they ... READ-FROM-STRING is in a different package. ...
    (comp.lang.lisp)
  • Re: Quick question: eval and read-from-string scoping
    ... At first I wondered why the local variables were not in scope for the eval'd code and was going to post, ... the current environment with all its local variables to eval. ... (defun get-left nil left) ... description language for OCR (optical character recognition) software but I'm implementing it in Lisp before I write a parser, so I 'eval' the descriptions as if read in from a text file. ...
    (comp.lang.lisp)