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

SubcommandSummary
chan putsWrites a string and a newline to a channel.
chan getsReads one line from a channel.
chan readReads the rest of a channel, or a count of characters.
chan eofTests whether the last read reached the end of a channel.
chan seekMoves the place of the next read or write of a channel.
chan tellGives the place of the next read or write of a channel.
chan flushWrites the buffered output of a channel.
chan closeCloses a channel, or one direction of it.
chan namesGives the names of the open channels.
chan configureGives or sets the options of a channel.
chan copyCopies the data of one channel to another.
chan pipeMakes a pipe and gives its two channels.
chan pendingGives the count of bytes that wait in a buffer of a channel.
chan blockedTests whether the last read of a channel stopped for want of data.
chan eventGives 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

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

See also