Previous: , Up: Miscellaneous   [Contents]


3.3.4 Reading Scripts from Files

When writing more complicated blocks, it may be preferable to store the code in a separate file. This prevents needing to escape certain characters and allows for using specialized editing functionality. To help with this, a read-from-file function is provided.

;; foo.rb contains the following contents:
;; print "Hello"
(block-result (run-block (make-block*
                          #:code (read-from-file "foo.rb")
                          #:language ruby))) ;=> "Hello"

Relative filenames are looked up relative to the current working directory, not the current Guile script. Use Guile’s current-filename function to look up relative to the current Guile script.

A macro, read-from-file*, is also provided. This macro accepts an optional 2nd argument. When this 2nd argument is truthy, the file is looked up relative to the current filename.

;; foo.rb is looked up relative to the script containing this
;; statement.
(read-from-file* "foo.rb" #t)
;; Identical to read-from-file.
(read-from-file* "foo.rb")