1:- module(with_output, [with_output_to/3, with_output_to_pl/2]).    2
    3:- use_module(library(docker/random_names)).    4
    5:- meta_predicate
    6    with_output_to(+, ?, 0),
    7    with_output_to_pl(?, 0).
 with_output_to(+FileType, ?Spec, :Goal) is semidet
Runs Goal with current_output pointing at a file with UTF-8 encoding. In (+, -, :) mode, creates a randomly-generated file with random new name unified at Spec. With Spec unbound, generates a random one-time name. Does not try to back-track in order to create a unique random name. Hence overwrites any existing file.

This is an arity-three version of with_output_to/2; same name, different arity. Writes the results of running Goal to some file given by Spec and FileType. Fails if Spec and FileType fail to specify a writable file location.

When Spec unbound, generates a random name. Binds the name to Spec.

   24with_output_to(FileType, Spec, Goal) :-
   25    var(Spec),
   26    !,
   27    random_name_chk(Spec),
   28    with_output_to(FileType, Spec, Goal).
   29with_output_to(FileType, Spec, Goal) :-
   30    absolute_file_name(Spec, File, [file_type(FileType), access(write)]),
   31    setup_call_cleanup(
   32        open(File, write, Out, [encoding(utf8)]),
   33        with_output_to(Out, Goal),
   34        close(Out)).
 with_output_to_pl(?Spec, :Goal) is semidet
Runs Goal with current_output pointing at a randomly-generated Prolog source file with UTF-8 encoding. In (+, :) mode, creates a Prolog file with name given by Spec.
   42with_output_to_pl(Spec, Goal) :-
   43    with_output_to(prolog, Spec, Goal).
   44
   45:- multifile user:prolog_file_type/2.   46:- dynamic user:prolog_file_type/2.   47:- public user:prolog_file_type/2.   48
   49user:prolog_file_type(txt, text)