chanchannels
The commands for channels: read, write, move, configure, and copy.
chan SUBCOMMAND ?ARG ...?
Description
The channel commands as one ensemble. A channel is a stream of bytes with a name, such as stdout or the result of open. Most subcommands work as the command of the same name: chan gets is gets, chan configure is fconfigure, and chan copy is fcopy.
A new interpreter has no channels: the host turns on the capability channels, and open of a file also needs file. Each line of text goes out with \n on each platform, and the default encoding of a file is UTF-8.
A unique prefix of a subcommand is the subcommand. The examples of this page run in an empty root folder, and most of them read the file a.txt with the one line hello.
Subcommands
| Subcommand | Summary |
|---|---|
chan puts | Writes a string and a newline to a channel. |
chan gets | Reads one line from a channel. |
chan read | Reads the rest of a channel, or a count of characters. |
chan eof | Tests whether the last read reached the end of a channel. |
chan seek | Moves the place of the next read or write of a channel. |
chan tell | Gives the place of the next read or write of a channel. |
chan flush | Writes the buffered output of a channel. |
chan close | Closes a channel, or one direction of it. |
chan names | Gives the names of the open channels. |
chan configure | Gives or sets the options of a channel. |
chan copy | Copies the data of one channel to another. |
chan pipe | Makes a pipe and gives its two channels. |
chan pending | Gives the count of bytes that wait in a buffer of a channel. |
chan blocked | Tests whether the last read of a channel stopped for want of data. |
chan event | Gives or sets the script that the event loop runs when a channel can be read or written. |
Returns
The result of the subcommand.
Examples
% set f [open a.txt w]; chan puts $f hello; chan close $f
> set f [open a.txt]
> set line [chan gets $f]
> chan close $f
> set line
hello
% lsort [chan names std*]
stderr stdin stdout
Errors
% chan bogus
unknown or ambiguous subcommand "bogus": must be close, configure, copy, eof, flush, gets, names, pipe, puts, read, seek, or tell
-errorcode TCL LOOKUP INDEX subcommand
% chan close nochan
can not find channel named "nochan"
-errorcode TCL LOOKUP CHANNEL nochan
Differences from Tcl
chanhas nocreate,push,pop,postevent, ortruncate, and in Tcl 9.1 noisbinary. The message of an unknown subcommand does not listblocked,event, andpending(issue 690).- On Windows a file of tclsh writes
\r\nby default, and ztcl writes\n(issue 691). - In Tcl 9.0.4 the messages of a wrong count of arguments name the channel
channel(issue 692).
Facts
- Capability
channels. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1