Prev: Lisp and ncurses
Next: check for non empty string
From: Cecil Westerhof on 19 Dec 2009 12:22 I have made a first setup to write interactive scripts with Common Lisp. In it I use the following code: (setq *outputfile* (open *outputfile-name* :direction :output :if-exists :supersede)) (if (null *outputfile*) (format t "Could not open outputfile (~a)~%" *outputfile-name*) (bye)) But the warning is not displayed. I think because the output is not flushed before the '(bye)'. How can flush the output so the error is shown? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: joswig on 19 Dec 2009 12:33 On 19 Dez., 18:22, Cecil Westerhof <Ce...(a)decebal.nl> wrote: > I have made a first setup to write interactive scripts with Common > Lisp. In it I use the following code: > (setq *outputfile* (open *outputfile-name* > :direction :output > :if-exists :supersede)) > (if (null *outputfile*) > (format t "Could not open outputfile (~a)~%" *outputfile-name*) > (bye)) > > But the warning is not displayed. I think because the output is not > flushed before the '(bye)'. How can flush the output so the error is > shown? > > -- > Cecil Westerhof > Senior Software Engineer > LinkedIn:http://www.linkedin.com/in/cecilwesterhof You might want to read a book like Practical Common Lisp to get an introduction: http://www.gigamonkeys.com/book/ Common Lisp has the HyperSpec online, where you can look for the basic provided functionality: Here is the 'Streams' dictionary: http://www.lispworks.com/documentation/lw50/CLHS/Body/c_stream.htm See WITH-OPEN-FILE, FINISH-OUTPUT and FORCE-OUTPUT. > (setq *outputfile* (open *outputfile-name* > :direction :output > :if-exists :supersede)) Use WITH-OPEN-FILE instead. > (if (null *outputfile*) If you set it, then the variable will not be NULL. > (format t "Could not open outputfile (~a)~%" *outputfile-name*) > (bye))
From: Cecil Westerhof on 19 Dec 2009 13:04 Cecil Westerhof <Cecil(a)decebal.nl> writes: > I have made a first setup to write interactive scripts with Common > Lisp. In it I use the following code: > (setq *outputfile* (open *outputfile-name* > :direction :output > :if-exists :supersede)) > (if (null *outputfile*) > (format t "Could not open outputfile (~a)~%" *outputfile-name*) > (bye)) Wanted to do things to fast. Lisp is a little different as other languages. The if uses only the first one, the rest is part of the else. So my program was already exited when I did check if the input file was correctly opened. I now made a function for it: (defun got-file-pointer(file-pointer message) (if (null file-pointer) (progn (format "~a~%" message) (bye)))) And use: (got-file-pointer *outputfile* "Could not open outputfile") after trying to open the file. I expected that if *outputfile-name* is nil (or has a value that does not represents a file that can be opened), that I would get back a nil value. But clisp opens a file. Which one I do not know. I have to dive into the file io of clisp. Of-course clisp is a real programming language and not a scripting language like bash (which I want to replace with clisp), so I need to change my state of mind when scripting with clisp. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Cecil Westerhof on 19 Dec 2009 13:16 "joswig(a)corporate-world.lisp.de" <joswig(a)lisp.de> writes: > You might want to read a book like Practical Common Lisp to > get an introduction: > > http://www.gigamonkeys.com/book/ > > Common Lisp has the HyperSpec online, where you can look > for the basic provided functionality: > > Here is the 'Streams' dictionary: > > http://www.lispworks.com/documentation/lw50/CLHS/Body/c_stream.htm Thanks for the pointers. > See WITH-OPEN-FILE, FINISH-OUTPUT and FORCE-OUTPUT. > >> (setq *outputfile* (open *outputfile-name* >> :direction :output >> :if-exists :supersede)) > > Use WITH-OPEN-FILE instead. > >> (if (null *outputfile*) > > If you set it, then the variable will not be NULL. I expected it to be set to nil if the file could not be opened. But that is not the case. An exception will be generated. The with-open-file is the way to go I think. The only 'disadvantage' is that when working with several files I will get a deep nesting. But I can live with that. Properly a better solution as I made myself. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: joswig on 19 Dec 2009 13:47
On 19 Dez., 19:04, Cecil Westerhof <Ce...(a)decebal.nl> wrote: > Cecil Westerhof <Ce...(a)decebal.nl> writes: > > I have made a first setup to write interactive scripts with Common > > Lisp. In it I use the following code: > > (setq *outputfile* (open *outputfile-name* > > :direction :output > > :if-exists :supersede)) > > (if (null *outputfile*) > > (format t "Could not open outputfile (~a)~%" *outputfile-name*) > > (bye)) > > Wanted to do things to fast. Lisp is a little different as other > languages. The if uses only the first one, the rest is part of the > else. So my program was already exited when I did check if the input > file was correctly opened. I now made a function for it: > (defun got-file-pointer(file-pointer message) Lisp has no pointers. If you read from a file you need a stream object. streams are created for example with OPEN and WITH-OPEN-FILE. (with-open-file (stream "/foo/bar.text" :direction :input) (read stream)) In some cases OPEN might return NIL, but with default settings you may see errors for many problems. For example a directory might not exist: CL-USER 224 > (open "/tmp1/foo" :direction :output) Error: Error while opening on stream #<STREAM::LATIN-1-FILE-STREAM / tmp1/foo>: error No such file or directory. 1 (continue) Try opening "/tmp1/foo" again. 2 (abort) Return to level 0. 3 Return to top loop level 0. Type :b for backtrace or :c <option number> to proceed. Type :bug-form "<subject>" for a bug report template or :? for other options. > (if (null file-pointer) > (progn (format "~a~%" message) > (bye)))) (when (foo) (form1) (form2) (form-n)) > > And use: > (got-file-pointer *outputfile* "Could not open outputfile") > after trying to open the file. > > I expected that if *outputfile-name* is nil (or has a value that does > not represents a file that can be opened), that I would get back a nil > value. But clisp opens a file. Which one I do not know. I have to dive > into the file io of clisp. > > Of-course clisp is a real programming language and not a scripting > language like bash (which I want to replace with clisp), so I need to > change my state of mind when scripting with clisp. Common Lisp is 'Common Lisp' or shorter 'CL'. CLISP is an implementation of Common Lisp, usually. > > -- > Cecil Westerhof > Senior Software Engineer > LinkedIn:http://www.linkedin.com/in/cecilwesterhof |