guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I/O, modules


From: Thien-Thi Nguyen
Subject: Re: I/O, modules
Date: Tue, 13 Nov 2012 10:53:29 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux)

() address@hidden
() Tue, 13 Nov 2012 00:57:11 -0500 (EST)

   [...] to read the whole file at once.

   Should I import some module to get the above functions? Which one?

Personally, i would do:
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Tue Nov 13 10:04:09

cd /tmp ; cat upcase ; guile -s upcase upcase to ; echo =/= ; cat to
;;; upcase

(use-modules (ttn-do mogrify))

(define (upcase-file i o)
  (call-with-output-file o
    (lambda (o)
      (display (string-upcase
                (editing-buffer (find-file-read-only i)
                  (buffer-string)))
               o))))

(apply upcase-file (cdr (command-line)))

;;; upcase ends here
=/=
;;; UPCASE

(USE-MODULES (TTN-DO MOGRIFY))

(DEFINE (UPCASE-FILE I O)
  (CALL-WITH-OUTPUT-FILE O
    (LAMBDA (O)
      (DISPLAY (STRING-UPCASE
                (EDITING-BUFFER (FIND-FILE-READ-ONLY I)
                  (BUFFER-STRING)))
               O))))

(APPLY UPCASE-FILE (CDR (COMMAND-LINE)))

;;; UPCASE ENDS HERE

Compilation finished at Tue Nov 13 10:04:09
which (trivially) uses module (ttn-do mogrify) for input:
<http://www.glug.org/software/ttn-do/ttn-do.html.gz#mogrify>

If you grep ttn-do, you'll see other examples, most of which do more
inside ‘editing-buffer’ than outside.  In various versions of Guile,
there is module ‘(ice-9 gap-buffer)’, which is "related technology".

   [...] problem with modules. How to check what can be imported?

You can ‘(set! %load-verbosely #t)’ to check.  On a deeper level, Guile
resolves a module name to a filename using var ‘%load-path’, which is
initialized from env var ‘GUILE_LOAD_PATH’ and command-line arg ‘-L DIR’
(one or more).  By default, cwd is excluded from these, for security.
Try to specify it explicitly, e.g.: "guile -L . -s import-test.scm".

At least, this is my understanding of Guile 1.x -- maybe 2.0 differs.

[clickety click]

Hmm, i see you've successfully tempted me into passing a half hour
playing w/ this stuff.  Well done!  Might as well share the fruit.
;;; upcase
;;;
;;; Usage: guile -s upcase INFILE OUTSTEM
;;;
;;; Description: Upcase INFILE to both OUTSTEM-unsafe, OUTSTEM-safe{1,2}.

(use-modules (ttn-do mogrify))

(define (upcase-file i o)

  ;; unsafe: Output fails silently if ‘display’ does short ‘write’.
  ;;         This occurs for some Guile versions, sometimes.
  (let ((o (string-append o "-unsafe")))
    (call-with-output-file o
      (lambda (o)
        (display (string-upcase
                  (editing-buffer (find-file-read-only i)
                    (buffer-string)))
                 o))))

  ;; safe1: As long as you have the memory.  :-D
  (let ((o (string-append o "-safe1")))
    (editing-buffer (find-file i)
      (let ((new (string-upcase (buffer-string))))
        (delete-region (point-min) (point-max))
        (insert new))
      (write-to-port (open-output-file o))))

  ;; safe2: Like safe1, but w/ a more functional style.
  (let ((o (string-append o "-safe2")))
    (editing-buffer (string-upcase
                     (editing-buffer (find-file-read-only i)
                       (buffer-string)))
      (write-to-port (open-output-file o))))

  ;; Add more shouted mumblings here.
  )

(apply upcase-file (cdr (command-line)))

;;; upcase ends here
Poking around in ~/codebits/scheme, i also found this:
#!/usr/local/bin/guile \
-e main -s
!#

;;; ID: $Id: upcase.scm,v 1.3 1999/05/15 10:15:31 ttn Exp $

(define (toupper s)
  (list->string
   (map (lambda (c)
          (if (char-lower-case? c)
              (char-upcase c)
              c))
        (string->list s))))

(define main
  (lambda args
    (let r ((form (read-line)))
      (or (eof-object? form)
          (begin
            (write-line (toupper form))
            (r (read-line)))))
    (quit)))

;;; $RCSfile: upcase.scm,v $$Revision: 1.3 $ ends here
It operates line at a time, which is nice for pipes, etc...  OK, enough!

-- 
Thien-Thi Nguyen ..................................... GPG key: 4C807502
.                  NB: ttn at glug dot org is not me                   .
.                 (and has not been since 2007 or so)                  .
.                        ACCEPT NO SUBSTITUTES                         .
........... please send technical questions to mailing lists ...........

Attachment: pgpYzyfnQiQ4O.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]