1puts(n)                      Tcl Built-In Commands                     puts(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       puts - Write to a channel
9

SYNOPSIS

11       puts ?-nonewline? ?channelId? string
12______________________________________________________________________________
13

DESCRIPTION

15       Writes  the  characters  given  by string to the channel given by chan‐
16       nelId.
17
18       ChannelId must be an identifier for an open channel such as a Tcl stan‐
19       dard channel (stdout or stderr), the return value from an invocation of
20       open or socket, or the result of a channel creation command provided by
21       a Tcl extension. The channel must have been opened for output.
22
23       If  no channelId is specified then it defaults to stdout. Puts normally
24       outputs a newline character after string, but this feature may be  sup‐
25       pressed by specifying the -nonewline switch.
26
27       Newline  characters  in  the output are translated by puts to platform-
28       specific end-of-line sequences according to the current  value  of  the
29       -translation  option  for the channel (for example, on PCs newlines are
30       normally replaced with  carriage-return-linefeed  sequences.   See  the
31       fconfigure  manual  entry  for a discussion on ways in which fconfigure
32       will alter output.
33
34       Tcl buffers output internally, so characters written with puts may  not
35       appear immediately on the output file or device;  Tcl will normally de‐
36       lay output until the buffer is full or the channel is closed.  You  can
37       force output to appear immediately with the flush command.
38
39       When  the  output buffer fills up, the puts command will normally block
40       until all the buffered data has been accepted for output by the operat‐
41       ing  system.  If channelId is in nonblocking mode then the puts command
42       will not block even if the operating system  cannot  accept  the  data.
43       Instead,  Tcl  continues  to buffer the data and writes it in the back‐
44       ground as fast as the underlying file or device can accept it.  The ap‐
45       plication  must  use the Tcl event loop for nonblocking output to work;
46       otherwise Tcl never finds out that the file or device is ready for more
47       output data.  It is possible for an arbitrarily large amount of data to
48       be buffered for a channel in nonblocking mode, which  could  consume  a
49       large  amount  of  memory.   To  avoid  wasting memory, nonblocking I/O
50       should normally be used in an event-driven fashion with  the  fileevent
51       command  (do not invoke puts unless you have recently been notified via
52       a file event that the channel is ready for more output data).
53

EXAMPLES

55       Write a short message to the console (or wherever stdout is directed):
56
57              puts "Hello, World!"
58
59       Print a message in several parts:
60
61              puts -nonewline "Hello, "
62              puts "World!"
63
64       Print a message to the standard error channel:
65
66              puts stderr "Hello, World!"
67
68       Append a log message to a file:
69
70              set chan [open my.log a]
71              set timestamp [clock format [clock seconds]]
72              puts $chan "$timestamp - Hello, World!"
73              close $chan
74

SEE ALSO

76       file(n), fileevent(n), Tcl_StandardChannels(3)
77

KEYWORDS

79       channel, newline, output, write
80
81
82
83Tcl                                   7.5                              puts(n)
Impressum