Packaging Reusable Libraries

While working on the e-mail database, you might write several functions related to storing and retrieving text that don’t have anything in particular to do with e-mail. You might realize that those functions could be useful in other programs and decide to repackage them as a library. You should define a new package, but this time you’ll export certain names to make them available to other packages.

  1. (defpackage :com.gigamonkeys.text-db
  2. (:use :common-lisp)
  3. (:export :open-db
  4. :save
  5. :store))

Again, you use the COMMON-LISP package, because you’ll need access to standard functions within COM.GIGAMONKEYS.TEXT-DB. The :export clause specifies names that will be external in COM.GIGAMONKEYS.TEXT-DB and thus accessible in packages that :use it. Therefore, after you’ve defined this package, you can change the definition of the main application package to the following:

  1. (defpackage :com.gigamonkeys.email-db
  2. (:use :common-lisp :com.gigamonkeys.text-db))

Now code written in COM.GIGAMONKEYS.EMAIL-DB can use unqualified names to refer to the exported symbols from both COMMON-LISP and COM.GIGAMONKEYS.TEXT-DB. All other names will continue to be interned directly in the COM.GIGAMONKEYS.EMAIL-DB package.