如何检查标准输入是否在 TCL 中可读? [英] How to check if stdin is readable in TCL?
本文介绍了如何检查标准输入是否在 TCL 中可读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下命令,您可以为 stdin
注册一些回调:
With the following command you can register some callback for stdin
:
fileevent stdin readable thatCallback
这意味着在执行更新命令期间,它会在 stdin
处有可用输入时一次又一次地评估 thatCallback
.
This means that during the execution of the update command it will evaluate thatCallback
time after time while there is input available at stdin
.
如何检查 stdin
是否有输入?
How can I check if input is available at stdin
?
推荐答案
您只需从回调中的 stdin 读取/获取即可.基本上,该模式类似于来自 Kevin Kenny 的 fileevent 示例中的这个片段:
You simply read/gets from stdin inside your callback. Basically the pattern goes like this snippet from the fileevent example from Kevin Kenny:
proc isReadable { f } {
# The channel is readable; try to read it.
set status [catch { gets $f line } result]
if { $status != 0 } {
# Error on the channel
puts "error reading $f: $result"
set ::DONE 2
} elseif { $result >= 0 } {
# Successfully read the channel
puts "got: $line"
} elseif { [eof $f] } {
# End of file on the channel
puts "end of file"
set ::DONE 1
} elseif { [fblocked $f] } {
# Read blocked. Just return
} else {
# Something else
puts "can't happen"
set ::DONE 3
}
}
# Open a pipe
set fid [open "|ls"]
# Set up to deliver file events on the pipe
fconfigure $fid -blocking false
fileevent $fid readable [list isReadable $fid]
# Launch the event loop and wait for the file events to finish
vwait ::DONE
# Close the pipe
close $fid
这篇关于如何检查标准输入是否在 TCL 中可读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文