I am trying to tail a file using fileevent
(Should only Tcl 8.4 version, so I can't use chan
command).
% proc GetData {chan} {
set data [read $chan]
puts "[string length $data] $data"
if {[eof $chan]} {
fileevent $chan readable {}
}
}
%
%
%
% set fp [open "|tail -f /home/dinesh/input" r+]
file7
% fconfigure $fp
-blocking 1 -buffering full -buffersize 4096 -encoding utf-8 -eofchar {{} {}} -translation {auto lf}
% fconfigure $fp -blocking 0 -buffering line
%
%
%
%
% fileevent $fp readable [list GetData $fp]
%
I have changed the file contents using cat
command in another terminal, but still GetData
is not getting called.
With gets $fp line
, I am able to get the modified contents, but why fileevent is not triggered ?
The callbacks scheduled with fileevent
only occur when you are running the event loop, and tclsh
doesn't run an event loop for you by default. Try doing this to see if my hypothesis is correct:
update
If it is, you'll need to design your program to work driven by callbacks. The standard way of running the event loop is this:
vwait forever
# Any global variable name would do, but “forever” has a nice feel
But that's a bit less helpful for your interactive testing. (One way round this is to load the Tk package; that turns on running the event loop by default so you don't need to do so explicitly. And gives you windows to manage, so it isn't without consequences…)