1puts(n) Tcl Built-In Commands puts(n)
2
3
4
5______________________________________________________________________________
6
8 puts - Write to a channel
9
11 puts ?-nonewline? ?channelId? string
12_________________________________________________________________
13
14
16 Writes the characters given by string to the channel given by chan‐
17 nelId.
18
19 ChannelId must be an identifier for an open channel such as a Tcl stan‐ │
20 dard channel (stdout or stderr), the return value from an invocation of │
21 open or socket, or the result of a channel creation command provided by │
22 a Tcl extension. The channel must have been opened for output.
23
24 If no channelId is specified then it defaults to stdout. Puts normally
25 outputs a newline character after string, but this feature may be sup‐
26 pressed by specifying the -nonewline switch.
27
28 Newline characters in the output are translated by puts to platform-
29 specific end-of-line sequences according to the current value of the
30 -translation option for the channel (for example, on PCs newlines are
31 normally replaced with carriage-return-linefeed sequences; on Macin‐
32 toshes newlines are normally replaced with carriage-returns). See the
33 fconfigure manual entry for a discussion on ways in which fconfigure
34 will alter output.
35
36 Tcl buffers output internally, so characters written with puts may not
37 appear immediately on the output file or device; Tcl will normally
38 delay output until the buffer is full or the channel is closed. You
39 can force output to appear immediately with the flush command.
40
41 When the output buffer fills up, the puts command will normally block
42 until all the buffered data has been accepted for output by the operat‐
43 ing system. If channelId is in nonblocking mode then the puts command
44 will not block even if the operating system cannot accept the data.
45 Instead, Tcl continues to buffer the data and writes it in the back‐
46 ground as fast as the underlying file or device can accept it. The
47 application must use the Tcl event loop for nonblocking output to work;
48 otherwise Tcl never finds out that the file or device is ready for more
49 output data. It is possible for an arbitrarily large amount of data to
50 be buffered for a channel in nonblocking mode, which could consume a
51 large amount of memory. To avoid wasting memory, nonblocking I/O
52 should normally be used in an event-driven fashion with the fileevent
53 command (don't invoke puts unless you have recently been notified via a
54 file event that the channel is ready for more output data).
55
57 Write a short message to the console (or wherever stdout is directed):
58 puts "Hello, World!"
59
60 Print a message in several parts:
61 puts -nonewline "Hello, "
62 puts "World!"
63
64 Print a message to the standard error channel:
65 puts stderr "Hello, World!"
66
67 Append a log message to a file:
68 set chan [open my.log a]
69 set timestamp [clock format [clock seconds]]
70 puts $chan "$timestamp - Hello, World!"
71 close $chan
72
73
75 file(n), fileevent(n), Tcl_StandardChannels(3)
76
77
79 channel, newline, output, write
80
81
82
83Tcl 7.5 puts(n)