chan , a built-in Tcl command, manipulates channels. Commands%|%built-in] Tcl command, manipulates channels.
chan provides various operations on a channel, including many that have been available using a mix of other commands. action indicates what to do with the channel. Any unique abbreviation for an action is acceptable. The valid actionss are:
AMG: chan aggregates all the commands you'll need for working with AMG: [chan] aggregates all the commands you'll need for working with and reflected channels). Use open or socket or an extension and reflected channels). Use [open] or [socket] or an extension as a chan subcommand, please report it. as a [chan] subcommand, please report it.
'''[chan read]''' ''channelId'' ?''numChars''?:
'''[chan read]''' ?'''-nonewline'''? ''channelId'': supercedes [read]
MG has just needed to find out how long the EOL sequence in a particular file was. Checking fconfigure $fid -translation didn't help (as it was just 'auto' on the read, and always crlf (Win XP) on the write, even if the file used Unix line-endings). Came up with this (which is possibly obvious, but I'm rather pleased with myself anyway;)
proc lineEndingSize {file} { set fid [open $file r] chan gets $fid line set size [expr {[chan tell $fid] - [string bytelength $line]}] close $fid return $size; }
DKF: That can go wrong. The problem is that you need to use the encoded length of the string read, and some encodings have multiple ways of encoding a particular character. This is all rather nasty; even guessing based on the value of fconfigure -encoding can go wrong! So instead try this:
proc lineEndingSize file { set f [open $file] gets $f line set after [tell $f] seek $f 0 read $f [string length $line] set before [tell $f] close $f return [expr {$after - $before}] }
MG: I'll try that instead, thanks :)
RS: History sometimes runs in circles... Tcl 2.1 didn't have commands dealing with channels. Peter da Silva added the "stream" extension, where one could write
stream fp open $filename r set x [stream fp gets] stream fp close
Later, the parts of "stream" went into the core as separate commands. Still later, in 8.5, they get reunited again in chan, which arguably makes the command set leaner, but scripts wordier...