[tbnl-devel] Re: [Lispweb] tbnl: what package?

Travis Cross travis at crosswirecorp.com
Fri Feb 10 17:23:35 UTC 2006


(we are now well outside of TBNL land...)

Jim Prewett wrote:
> When you visit /foo, the webpage returned displays this text: 
> "package: #<The COMMON-LISP-USER package>"

I experienced a nasty bug awhile back caused by similar confusion.
I was reading from a stream at runtime and expecting the package to
be bound to the package in effect in the source code.  Wrong.  Or at
least, wrong some of the time, which caused a debugging nightmare:
the code worked correctly when started from SLIME, but failed when
SBCL was started by my init scripts.  It took me some time to
understand what was going on.

> What I'm not understanding is *why* that is what is displayed.

The explanation is this: IN-PACKAGE only binds *package* at
compile-time.  At runtime, it is your responsibility to ensure the
correct package is dynamically bound.

The hyperspec does not make this point very clearly at all.

If you are reading symbols at runtime for the purpose of comparison,
I recommend using the keyword package.  The example then becomes
something more akin to:

(defpackage :mypackage (:use :cl))
(in-package :mypackage)

(defun some-func ()
  (cl-who:with-html-output-to-string
      (*standard-output* nil :prologue nil :indent nil)
    (:html
     (:head
      (:title "foo"))
     (:body
      (cl-who:str
       (cl-who:escape-string
        (let ((*package* (find-package :keyword)))
          (format nil "package: ~a~%query=BAR?: ~a~%" *package*
                  (eq :bar (intern (or (tbnl:parameter "query")
                                       "BAR")))))))))))

(setq tbnl:*dispatch-table*
      (list (tbnl:create-prefix-dispatcher
             "/tbnl/foo" #'some-func)))

Cheers,

-- Travis



More information about the Tbnl-devel mailing list