(index ("call-with-environment-variables" 0))
(def (sig (procedure "(call-with-environment-variables variables thunk) → unspecified" (id call-with-environment-variables))) (p "Sets up environment variable via dynamic-wind which are taken down after thunk.") (dl (dt (tt "variables")) (dd "An alist of the form " (tt "'((\"var\" . \"value\") ...)")) (dt (tt "thunk")) (dd "The thunk to execute with a modified environment")) (highlight scheme "(define (call-with-environment-variables variables thunk)\n  (let ((pre-existing-variables\n          (map (lambda (var-value)\n                 (let ((var (car var-value)))\n                   (cons var (get-environment-variable var))))\n               variables)))\n    (dynamic-wind\n      (lambda () (void))\n      (lambda ()\n        (use posix)\n        (for-each\n          (lambda (var-value) (setenv (car var-value) (cdr var-value)))\n          variables)\n        (thunk))\n      (lambda ()\n        (for-each\n          (lambda (var-value)\n            (let ((var (car var-value)) (value (cdr var-value)))\n              (if value (setenv var value) (unsetenv var))))\n          pre-existing-variables)))))"))
