[plt-scheme] eval in webserver code

From: Gunnar J. Árnason (gunnarja at internet.is)
Date: Fri Jan 28 10:17:16 EST 2005

Hi, I’m new to the list (although I have mined it recently for information)
and am trying to learn scheme with the PLT tools and so far it has been an
enjoyable exprience. 

I came across something that puzzles me. I wrote a utility function to
simplify some webserver code and although it works in the DrScheme
environment it doesn't work in the webserver code. I have some structures
stored in a hash table and want to access the fields of the structure for
publication on the web page. So I came up with this: 

(define (print-field id field)
     (format "~a"
             (eval `(,(string->symbol (format "individual-~a" field))
                     ,(hash-table-get *index* ',id)))))

Which results in the error message: 

Servlet exception: reference to undefined identifier: individual-name

I solved this instead by using the following, which works fine: 

(define (print-field id field)
     (case field
       [(name) (format "~a" (individual-name (hash-table-get *index* id)))]
       [(dob) (format "~a" (individual-dob (hash-table-get *index* id)))]
       [else (format "~a is not a valid field" field)]))

I'm using v209 and the webpage code looks like this (slightly abbreviated):

(define (individual-record id)
     (send/suspend/dispatch
      (lambda (embed/url)
        `(html (head (title "Individual"))
          (body (h1 "Information about individual")
           (table
            (tr (td "Name") (td ,(print-field id 'nafn)))
            (tr (td "Date of birth") (td ,(print-field id 'far)))))))))

Can anyone explain to me why the shorter utility function using eval doesn't
work?

Thanks, Gunnar





Posted on the users mailing list.