This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: loading files locally
- To: Klaus.Schilling AT home.ivm.de
- Subject: Re: loading files locally
- From: Mikael Djurfeldt <mdj AT mdj-pc.nada.kth.se>
- Date: 16 Sep 1999 18:41:19 +0200
- Cc: guile AT sourceware.cygnus dot com
- Cc: djurfeldt AT nada.kth.se
- References: <m11RkMn-000H3wC@debian>
Klaus Schilling <Klaus.Schilling@home.ivm.de> writes:
> is there a way to load files in a way that they get evaluated
> in the current environment instead of the global one?
Yes, from Kent R. Dybvigs book "The Scheme Programming Language"
(http://www.scheme.com/tspl2ed.html):
(use-modules (ice-9 syncase))
(define-syntax include
(lambda (x)
(define read-file
(lambda (fn k)
(let ((p (open-input-file fn)))
(let f ((x (read p)))
(if (eof-object? x)
(begin (close-input-port p) '())
(cons (datum->syntax-object k x)
(f (read p))))))))
(syntax-case x ()
((k filename)
(let ((fn (syntax-object->datum (syntax filename))))
(with-syntax (((exp ...) (read-file fn (syntax k))))
(syntax (begin exp ...))))))))
If I now put:
(set! a 2)
in "/tmp/a.scm", the following expression:
(let ((a 1))
(include "/tmp/a.scm")
a)
yields the result
2