A task related to printing information is that of printing error messages. It is a good idea to print error messages in such a way that they can be easily separated from the normal output of the program. In particular, error messages go to the standard error stream while normal output goes to the standard output stream.
The benefit of sending errors to a separate stream is that, once separated, the two streams can be easily sent to different places. Typically, the standard output is redirected to a file or piped to another process while the standard error remains undirected so that it can be seen at the terminal. For example, the command "
cat foo | wc" (to count the words in
foo) will print "
foo: no such file" if
foo does not exist. You see the error message because the standard error is not redirected. If the diagnostic was sent along with the normal output of
wc would report that
foo had four words only because that is how many are in the error message!
Expect scripts should be written the same way. Normal output that you might wish to redirect or pipe should be sent to the standard output. Error messages should be sent to the standard error. There are two ways of doing this:
send_error and "
send_error is similar to
send whereas "
puts stderr" is similar to
I will show more differences later. To summarize the differences so far between variations on
puts and the variations on
automatically appends newline ...