macro
boot.janet on line 374, column 1
(with [binding ctor dtor] & body)
Evaluate body with some resource, which will be automatically
cleaned up if there is an error in body. binding is bound to the
expression ctor, and dtor is a function or callable that is passed
the binding. If no destructor (dtor) is given, will call :close on
the resource.
# Setup
(spit "poem.txt" "I’ve decided to tackle it\nIt’s going to get done")
# A version of head -n 1, but entirely in Janet
# Note, that because files in Janet support :close as a method
# we don't have to define a dtor.
# There might be other closing work to do, of course
(with [f (file/open "poem.txt")] (print (:read f :line))) # => "I've decided to tackle it"
(os/shell "echo bar > /tmp/foo")
(with
[file-handle
(file/open "/tmp/foo")
(fn [fd] (file/close fd))]
(file/read file-handle :all)) # => @"bar\n"