打印原始路径名结构

时间:2016-09-08 13:38:34

标签: common-lisp

使用CCL,当我使用例如(format t "~s" pathname)pprintprint打印路径名时,会使用#P阅读器语法打印出来。例如:

? (make-pathname :directory "foo")
#P"foo/"
? (format t "~s" (make-pathname :directory "foo"))
#P"foo/"
NIL

我真的很想看到底层的路径名结构,这样我就能确切地知道对象的样子。有没有办法将它打印出来?

3 个答案:

答案 0 :(得分:7)

我不知道这是不是你想要的,但你可以call the inspector

(inspect thing)

CCL示例:

? (inspect (make-pathname :directory "foo"))
[0]     #P"foo/"
[1]     Type: PATHNAME
[2]     Class: #<BUILT-IN-CLASS PATHNAME>
[3]     TYPE: (PATHNAME . #<CCL::CLASS-WRAPPER PATHNAME #x14083886>)
[4]     %PATHNAME-DIRECTORY: (:RELATIVE "foo")
[5]     %PATHNAME-NAME: NIL
[6]     %PATHNAME-TYPE: NIL
[7]     %PHYSICAL-PATHNAME-VERSION: NIL
[8]     %PHYSICAL-PATHNAME-DEVICE: NIL
Inspect> help
The following toplevel commands are available:
 <n>    the same as (:I <n>)
 (:S N V)  set the <n>th line of object data to value <v>
 :HOME   show first page of object data
 :PREV   show previous page of object data
 :NEXT   show next page of object data
 :SHOW   re-show currently inspected object (the value of CCL:@)
 :Q     exit inspector
 :POP   exit current inspector level
 (:I N)  inspect <n>th item
 :?     help
 :PWD   Print the pathame of the current directory
 (:CD DIR)  Change to directory DIR (e.g., #p"ccl:" or "/some/dir")
 (:PROC &OPTIONAL P)  Show information about specified process <p>/all processes
 (:KILL P)  Kill process whose name or ID matches <p>
 (:Y &OPTIONAL P)  Yield control of terminal-input to process
whose name or ID matches <p>, or to any process if <p> is null
Any other form is evaluated and its results are printed out.
Inspect> 

Example on ideone with CLISP

答案 1 :(得分:7)

除了inspect之外,您还可以使用describe

? (describe #P"/tmp/**/file.*")
#P"/tmp/**/file.*"
Type: PATHNAME
Class: #<BUILT-IN-CLASS PATHNAME>
TYPE: (PATHNAME . #<CCL::CLASS-WRAPPER PATHNAME #x30004003ED0D>)
%PATHNAME-DIRECTORY: (:ABSOLUTE "tmp" :WILD-INFERIORS)
%PATHNAME-NAME: "file"
%PATHNAME-TYPE: :WILD
%PHYSICAL-PATHNAME-VERSION: :NEWEST
%PHYSICAL-PATHNAME-DEVICE: NIL

答案 2 :(得分:1)

这是格式指令的问题,将“S”更改为打印字符串的“A”,“S”(尝试)打印可读取REPL或读取函数的有效对象

; SLIME 2016-04-19
CL-USER> (format t "~s" (make-pathname :directory "foo"))
#P"/foo/"
NIL
CL-USER> (format t "~A" (make-pathname :directory "foo"))
/foo/
NIL

来自教程a few format receipes

  

“~S”尝试生成可以使用READ读回的输出。   因此,字符串将用引号括起来,符号将是   必要时包装合格,等等。没有的对象   使用不可读的对象语法打印可读表示   “&LT;取代。”使用冒号修饰符,〜A和~S指令都将NIL作为   ()而不是NIL。 ~A和~S指令也都需要   四个前缀参数,可用于控制填充是否>在(或在使用at-sign修饰符之前)添加值,但是那些   参数仅对生成表格数据非常有用。

最后只是将字符串放入nil而不是重定向到标准输出

CL-USER> (format nil "~A" (make-pathname :directory "foo"))
"/foo/"