Subject: using with_stdout using a variable for filename
From: Robert Dodier
Date: Tue, 20 Mar 2007 20:03:49 -0700
To cut to the chase -- I propose below that with_stdout should
evaluate its first argument. Yes/no ?
On 3/20/07, Robert Gloeckner <Robert.Gloeckner at gmx.net> wrote:
> i am trying to write data to a file like this:
>
> define_variable( gnuplot_temp_file, "maxima-temp.dat", string);
> with_stdout( gnuplot_temp_file, "hello");
>
> but that does not seem to work, because with_stdout is a macro.
> how can i use with_stdout with a variable filename?
At the interactive prompt you can write
with_stdout (''gnuplot_temp_file, print (foo, bar, baz));
Those are two single quotes (the so-called quote-quote operator),
not a double quote mark, before gnuplot_temp_file.
Quote-quote is applied by the input parser. So it could have an
unexpected effect if it appears within a function, for example,
because it causes an extra evaluation when the function is
defined, not when it is evaluated.
Here is a longer solution that applies the extra evaluation when
with_stdout is evaluated, not when it is input.
my_with_stdout (fn, [L]) ::= buildq ([fn:ev(fn), L], with_stdout
(fn, splice (L)));
e.g.
F (namebase, n) := my_with_stdout (concat (namebase, n), print (foo,
bar, baz));
F ("/tmp/foo", 3);
printfile ("/tmp/foo3");
=> foo bar baz
Notes (1) You can probably omit the define_variable. Maxima doesn't
really know much about that. (2) with_stdout only captures stuff which
is printed (not stuff shown by the interactive display). E.g. stuff from
print and grind is captured. (3) We should probably just change
with_stdout to evaluate its first argument, to avoid this problem in
the future. I think using a variable for a filename is a very common idea.
HTH
Robert