[hunchentoot-devel] How to pass define-easy-handler symbol?

This is probably a dumb question, but I haven't been able to figure it out yet. Suppose I want to call 'DEFINE-EASY-HANDLER inside another function and pass the symbol as an argument. I tried stuff like this: (defun some-function (name) (define-easy-handler ((intern name) (concatenate 'string name ".html") :default-request-type :post) ((formarg :parameter-type :keyword) (html-generator name formarg))) But it wants a real symbol. Then I tried passing names as symbols and turning them into strings where I needed. That works in the sense that it creates the page, but each one is bound to the same symbol no matter what symbols I give it, so only the last page created works. Is there a way to do this? Thanks for any help. --Jeff

Jeffrey Cunningham wrote:
Suppose I want to call 'DEFINE-EASY-HANDLER inside another function and pass the symbol as an argument. I tried stuff like this:
(defun some-function (name) (define-easy-handler ((intern name) :uri (concatenate 'string name ".html") :default-request-type :post) ((formarg :parameter-type :keyword)) (html-generator name formarg)))
define-easy-handler is a macro, which means that (intern name) doesn't get evaluated unless/until the macro sees fit. Try defining some-function as a macro too: (defmacro some-function (name) `(define-easy-handler (,(intern name) :uri ,(concatenate 'string name ".html") :default-request-type :post) ((formarg :parameter-type :keyword)) (html-generator ,name formarg))) Toby

On Fri Jan 12, 2007 at 04:13:08PM +0100, Toby wrote:
define-easy-handler is a macro, which means that (intern name) doesn't get evaluated unless/until the macro sees fit.
Try defining some-function as a macro too:
(defmacro some-function (name) `(define-easy-handler (,(intern name) :uri ,(concatenate 'string name ".html") :default-request-type :post) ((formarg :parameter-type :keyword)) (html-generator ,name formarg)))
That works. I tried a macro but didn't quite get it right. Thank you, Toby. --Jeff

On Fri Jan 12, 2007 at 04:13:08PM +0100, Toby wrote:
define-easy-handler is a macro, which means that (intern name) doesn't get evaluated unless/until the macro sees fit.
Try defining some-function as a macro too:
(defmacro some-function (name) `(define-easy-handler (,(intern name) :uri ,(concatenate 'string name ".html") :default-request-type :post) ((formarg :parameter-type :keyword)) (html-generator ,name formarg)))
Hmmm. Unfortunately, it leads to another problem when I try to apply it to something useful, such as this reduction: mapc #'some-function '("foo" "bar") or even (let ((s "foo")) (some-function s)) Now the macro sees only the symbol s, not the string it refers to. Is there a way to get around this? My knowledge of macros is pretty weak. Thanks, --Jeff

Jeffrey Cunningham wrote:
Now the macro sees only the symbol s, not the string it refers to. Is there a way to get around this?
Not without understanding how macros work. May I suggest this book, especially chapter 8? http://www.gigamonkeys.com/book/ Toby

On Sat Jan 13, 2007 at 06:27:22PM +0100, Toby wrote:
Jeffrey Cunningham wrote:
Now the macro sees only the symbol s, not the string it refers to. Is there a way to get around this?
Not without understanding how macros work.
May I suggest this book, especially chapter 8? http://www.gigamonkeys.com/book/
Okay, I read chapter 8 (very good chapter). And I'm almost there. I came up with this macro, which works: (defmacro build-handler (category) (let ((symb (gensym))) `(progn (setf ,symb (intern, category)) (define-easy-handler (,symb :uri (catstr "/" ,category ".html") :default-request-type :post) ((arg)) (declare (ignorable arg)) (with-html (:html (:body (:h1 "Crazy as " (str ,category)))))))))) So (let ((s "batshit")) (build-handler s)) and (build-handler "batshit") both work. But they give the following warning: -+ Warnings (2) |-- --> PROGN SETF | ==> | (SETQ #:G0 (INTERN CL-USER::S)) | undefined variable: #:G0 `-- This variable is undefined: #:G0 which doesn't seem healthy. Could you tell me what I'm doing wrong? Regards, --Jeff
participants (2)
-
Jeffrey Cunningham
-
Toby