Aliasses ?

Discussion of Common Lisp
Post Reply
mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Aliasses ?

Post by mcc » Tue Mar 31, 2015 10:04 am

Hi,

I want to use cl-ppcres and it seems, that I am doing something wrong
-- may be because I am think more perl (or whatelse) than LISP.

In my program I did a

Code: Select all

(require "cl-ppcre")
Since there are call that I have to prepend with "cl-ppcre::" every
time I use them, I looked for a way to shorten things with an "alias"
ore something like that.

I found "defalias" but that seems to be for elisp (Emacs).
Is there any other "legal" :( way to to something like that
i CL ?

Thank you very much in advance for any help!
Best regards,
Meino

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Aliasses ?

Post by edgar-rft » Tue Mar 31, 2015 12:48 pm

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

mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Re: Aliasses ?

Post by mcc » Tue Mar 31, 2015 8:06 pm

Hi edgar,

THANK YOU VERY MUCH FOR ALL THIS GOOD STUFF! :) :) :)

The problem, what I have right in the beginning of learning and using
Common Lisp is to know (or better: "not knowing") what I am looking
for. I /thought/ (and I was wrong) that (in this case) it has something
to do with Aliasses. I argued this from various shells mechanisms.
I searched for something like that and found "defalias" for elisp.
And then I asked my question (this thread). *BUT* It had something to
do with "package management" and symbols and scopes.
*FOR THIS* I had not searched for ....

Best regards,
mcc

Post Reply