[plt-scheme] eval and servlets

From: David J. Neu (djneu at att.net)
Date: Sun Oct 3 08:14:47 EDT 2004

Hi all,

I was wondering if someone could explain why in the following code,
the commented out version of _start_ fails, with error:

    Servlet exception: "reference to undefined identifier: square"

in both a module-based and a unit-based servlet.

The "same" function works fine outside of a servlet.

Something with eval and namespaces?

Many thanks!
--David

(module act-test
  mzscheme

  (provide interface-version timeout start)
  
  (require (lib "servlet-sig.ss" "web-server")
           (lib "servlet.ss" "web-server")           
           (lib "servlet-helpers.ss" "web-server"))
  
  (define interface-version 'v1)

  (define timeout +inf.0)

  (define square (lambda (x) (* x x)))

  (define ops
    `((s . ,(lambda (x) (square x)))
      (i . ,(lambda (x) (add1 x)))))

   ; this fails
;  (define start
;    (lambda (arequest)
;      (let ((lfile (open-input-file "act.ss")))
;        (let ((ops (eval (read lfile))))
;          (close-input-port lfile)
;          `(html
;            ,(format "~a" ((cdr (assq 's ops)) 2)))))))

  ; this works
  (define start
    (lambda (arequest)
      `(html
        ,(format "~a" ((cdr (assq 's ops)) 2))))))


;; start: act.ss
`((s . ,(lambda (x) (square x)))
  (i . ,(lambda (x) (add1 x))))
;; end: act.ss


Posted on the users mailing list.