Here is a full trancript of what I'm doing. First of all I load CL-PPRCE using Quicklisp::
Code: Select all
CL-USER> (ql:quickload :cl-ppcre)
To load "cl-ppcre":
Load 1 ASDF system:
cl-ppcre
; Loading "cl-ppcre"
(:CL-PPCRE)
If I use PPCRE functions without the ppcre: package prefix I get the following error:
Code: Select all
CL-USER> (parse-string "(ab)*")
The function COMMON-LISP-USER::PARSE-STRING is undefined.
[Condition of type UNDEFINED-FUNCTION]
Only if I write the function name with the ppcre: package prefix it works as expected:
Code: Select all
CL-USER> (ppcre:parse-string "(ab)*")
(:GREEDY-REPETITION 0 NIL (:REGISTER "ab"))
To get rid of the package prefix, Common Lisp provides the function
USE-PACKAGE for using the symbols of a package without writing the package prefix every time anew:
Code: Select all
CL-USER> (use-package :cl-ppcre)
T
Now I can call the PARSE-STRING function without the package prefix:
Code: Select all
CL-USER> (parse-string "(ab)*")
(:GREEDY-REPETITION 0 NIL (:REGISTER "ab"))
But this is not a really good idea because USE-PACKAGE loads a lot of symbols into the CL-USER package, and if I use several libraries at once the chance is good for getting symbol-name conflicts if two or more libraries use the same function or variable name.
Defining your own package is a better strategy that is generally recommended if you write programs using external libraries. Here I define my own package named MY-PACKAGE:
Code: Select all
CL-USER> (defpackage :my-package
(:use :common-lisp :cl-ppcre))
#<PACKAGE "MY-PACKAGE">
Caution: If you forget to :use :common-lisp, then you need to write things like (cl:print..) instead of (print...)
Here is how I switch into my own package:
Code: Select all
CL-USER> (in-package :my-package)
#<PACKAGE "MY-PACKAGE">
In Emacs using SLIME the prompt now changes from CL-USER> to MY-PACKAGE>. If your prompt doesn't tell you the package you're in then look at the value of the Common Lisp *package* variable:
Code: Select all
MY-PACKAGE> *package*
#<PACKAGE "MY-PACKAGE">
Because I have defined my package with (:use :common-lisp :cl-ppcre), I can use the symbols of the COMMON-LISP and CL-PPCRE packages with no need to write their package prefixes:
Code: Select all
MY-PACKAGE> (print (parse-string "(ab)*"))
(:GREEDY-REPETITION 0 NIL (:REGISTER "ab")) ; printed
(:GREEDY-REPETITION 0 NIL (:REGISTER "ab")) ; returned
To switch back into the CL-USER package:
Code: Select all
MY-PACKAGE> (in-package :cl-user)
#<PACKAGE "COMMON-LISP-USER">
As usual, the full story is in
Practical Common Lisp, Chapter 21:
Packages and Symbols.
- edgar