Caché 实际上支持任意 IO 重定向。这可以通过未记录的功能实现。不推荐,但不太可能改变。
注释代码如下。在此示例中,我选择将 IO 重定向到 %Stream.GlobalBinary - 您可以随意使用它。关键是标签的具体名称 - 您可以使用 use $io:: 调用指定不同的例程,并在其他地方指定这些标签。
//The ProcedureBlock = 0 is important
// it allows for calling of labels within the ClassMethod
ClassMethod testIORedirection() [ ProcedureBlock = 0 ]
{
//Create a stream that we will redirect to
set myStream = ##class(%Stream.GlobalBinary).%New()
//Redirect IO to the current routine - makes use of the labels defined below
use $io::("^"_$ZNAME)
//Enable redirection
do ##class(%Device).ReDirectIO(1)
//Any write statements here will be redirected to the labels defined below
write "Here is a string", !, !
write "Here is something else", !
//Disable redirection
do ##class(%Device).ReDirectIO(0)
//Print out the stream, to prove that it worked
write "Now printing the string:", !
write myStream.Read()
//Labels that allow for IO redirection
//Read Character - we don't care about reading
rchr(c) quit
//Read a string - we don't care about reading
rstr(sz,to) quit
//Write a character - call the output label
wchr(s) do output($char(s)) quit
//Write a form feed - call the output label
wff() do output($char(12)) quit
//Write a newline - call the output label
wnl() do output($char(13,10)) quit
//Write a string - call the output label
wstr(s) do output(s) quit
//Write a tab - call the output label
wtab(s) do output($char(9)) quit
//Output label - this is where you would handle what you actually want to do.
// in our case, we want to write to myStream
output(s) do myStream.Write(s) quit
}