| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH puts n 7.5 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
puts \- Write to a channel |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBputs \fR?\fB\-nonewline\fR? ?\fIchannelId\fR? \fIstring\fR |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
Writes the characters given by \fIstring\fR to the channel given |
| 20 |
by \fIchannelId\fR. |
| 21 |
.PP |
| 22 |
\fIChannelId\fR must be an identifier for an open channel such as a |
| 23 |
Tcl standard channel (\fBstdout\fR or \fBstderr\fR), the return |
| 24 |
value from an invocation of \fBopen\fR or \fBsocket\fR, or the result |
| 25 |
of a channel creation command provided by a Tcl extension. The channel |
| 26 |
must have been opened for output. |
| 27 |
.PP |
| 28 |
If no \fIchannelId\fR is specified then it defaults to |
| 29 |
\fBstdout\fR. \fBPuts\fR normally outputs a newline character after |
| 30 |
\fIstring\fR, but this feature may be suppressed by specifying the |
| 31 |
\fB\-nonewline\fR switch. |
| 32 |
.PP |
| 33 |
Newline characters in the output are translated by \fBputs\fR to |
| 34 |
platform-specific end-of-line sequences according to the current |
| 35 |
value of the \fB\-translation\fR option for the channel (for example, |
| 36 |
on PCs newlines are normally replaced with carriage-return-linefeed |
| 37 |
sequences. |
| 38 |
See the \fBfconfigure\fR manual entry for a discussion on ways in |
| 39 |
which \fBfconfigure\fR will alter output. |
| 40 |
.PP |
| 41 |
Tcl buffers output internally, so characters written with \fBputs\fR |
| 42 |
may not appear immediately on the output file or device; Tcl will |
| 43 |
normally delay output until the buffer is full or the channel is |
| 44 |
closed. |
| 45 |
You can force output to appear immediately with the \fBflush\fR |
| 46 |
command. |
| 47 |
.PP |
| 48 |
When the output buffer fills up, the \fBputs\fR command will normally |
| 49 |
block until all the buffered data has been accepted for output by the |
| 50 |
operating system. |
| 51 |
If \fIchannelId\fR is in nonblocking mode then the \fBputs\fR command |
| 52 |
will not block even if the operating system cannot accept the data. |
| 53 |
Instead, Tcl continues to buffer the data and writes it in the |
| 54 |
background as fast as the underlying file or device can accept it. |
| 55 |
The application must use the Tcl event loop for nonblocking output |
| 56 |
to work; otherwise Tcl never finds out that the file or device is |
| 57 |
ready for more output data. |
| 58 |
It is possible for an arbitrarily large amount of data to be |
| 59 |
buffered for a channel in nonblocking mode, which could consume a |
| 60 |
large amount of memory. |
| 61 |
To avoid wasting memory, nonblocking I/O should normally |
| 62 |
be used in an event-driven fashion with the \fBfileevent\fR command |
| 63 |
(do not invoke \fBputs\fR unless you have recently been notified |
| 64 |
via a file event that the channel is ready for more output data). |
| 65 |
.SH EXAMPLES |
| 66 |
.PP |
| 67 |
Write a short message to the console (or wherever \fBstdout\fR is |
| 68 |
directed): |
| 69 |
.PP |
| 70 |
.CS |
| 71 |
\fBputs\fR "Hello, World!" |
| 72 |
.CE |
| 73 |
.PP |
| 74 |
Print a message in several parts: |
| 75 |
.PP |
| 76 |
.CS |
| 77 |
\fBputs\fR -nonewline "Hello, " |
| 78 |
\fBputs\fR "World!" |
| 79 |
.CE |
| 80 |
.PP |
| 81 |
Print a message to the standard error channel: |
| 82 |
.PP |
| 83 |
.CS |
| 84 |
\fBputs\fR stderr "Hello, World!" |
| 85 |
.CE |
| 86 |
.PP |
| 87 |
Append a log message to a file: |
| 88 |
.PP |
| 89 |
.CS |
| 90 |
set chan [open my.log a] |
| 91 |
set timestamp [clock format [clock seconds]] |
| 92 |
\fBputs\fR $chan "$timestamp - Hello, World!" |
| 93 |
close $chan |
| 94 |
.CE |
| 95 |
.SH "SEE ALSO" |
| 96 |
file(n), fileevent(n), Tcl_StandardChannels(3) |
| 97 |
.SH KEYWORDS |
| 98 |
channel, newline, output, write |