Main Content

stdout v stderr (Tcl, Perl, Shell)

Archive - Originally posted on "The Horse's Mouth" - 2007-12-10 14:56:14 - Graham Ellis

When you're programming, you shouldn't write code to read directly from the keyboard and write to the screen .... what if you want to have your program read from or write to a file sometimes? Instead, you should write code to read from stdin (Tcl) or STDIN (Perl) and write to stdout (Tcl) or STDOUT (Perl). Then you can re-direct from and to file!

But wait .... if you redirect ALL your output to a file, including error messages, you'll find it pretty irritating having to look in a file to see if the program worked. Better to use the alternative stderr (Tcl) or STDERR (Perl) output channel for warnings, errors, and status reports.

Here's a Tcl example of code written that way:

puts -nonewline stderr "Who are you? "
flush stderr
set yername [gets stdin]
 
puts "Hello $yername and welcome"
puts stderr "Job Completed"


And here's an example of that program running:


arth-wind-and-fire:~/dec07 grahamellis$ tclsh tt
Who are you? Graham
Hello Graham and welcome
Job Completed
earth-wind-and-fire:~/dec07 grahamellis$ tclsh tt > xx.txt
Who are you? Graham
Job Completed
earth-wind-and-fire:~/dec07 grahamellis$ cat xx.txt
Hello Graham and welcome
earth-wind-and-fire:~/dec07 grahamellis$