guile-user
[Top][All Lists]
Advanced

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

Re: modules as scripts


From: Alex Schroeder
Subject: Re: modules as scripts
Date: Mon, 17 Sep 2001 00:33:59 +0200
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/20.7

Michael Livshin <address@hidden> writes:

> #!/bin/sh
> # aside from this initial boilerplate, this is actually -*- scheme -*- code
> main="(module-ref (resolve-module '(scripts snarf-check-and-output-texi)) 
> 'main)"
> exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
> !#
>
> the module has to have a procedure named `main', and there's even no
> need to export it.

Thanks for the pointer.

I experimented a bit with this, and ran into another problem.  The
data or scheme program is a file with a sexp such as (report ...).

"report" is a function in the module itself.  There for the "main"
function will read the data file, and the sexp has to be evaluated in
the top-level environment (I hope I am using the terms correctly).  In
the following example, I get the error that the variable report is
undefined.

Here is the setup.  Two files, the data file "test" and the
script/module "my-test".

test
----

(report "Alex")

my-test
-------

#!/bin/sh
# aside from this initial boilerplate, this is actually -*- scheme -*- code
main="(module-ref (resolve-module '(my-test)) 'main)"
exec ${GUILE-guile} -l $0 -c "(apply $main (cdr (command-line)))" "$@"
!#

(define-module (my-test)
  :export (my-test report))

(define (report name)
  (display "My Name is ")
  (display name)
  (newline))

(define (my-test . args)
  (eval
   (call-with-input-file "test" read)
   (interaction-environment)))

(define main my-test)



And here is the result:

address@hidden:~/projects/nulligarchie/tmp > my-test 
ERROR: Unbound variable: report

Eventhough this would have worked if I included the test data in the
script itself, it doesn't work when I need to *load* the data file.
The following would have worked:

(define (my-test . args)
  (report "Alex"))

My current solution splits it up into two files:

simple.scm -- the module
------------------------

(define-module (simple))

(define-public (report name)
  (display "My Name is ")
  (display name)
  (newline))

run-simple.scm -- the script
----------------------------

#! /usr/local/bin/guile \
-e main -s
!#

(use-modules (simple))

(define (main . args)
  (load "test"))


Alex.
-- 
http://www.geocities.com/kensanata/
Coffee should be black as hell, strong as death and sweet as love.
        -- Turkish proverb



reply via email to

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