Version 3 of close

Updated 2002-11-19 18:19:19

http://purl.org/tcl/home/man/tcl8.4/TclCmd/close.htm


LV What are the ramifications if you don't close a file in Tcl? Any? I assume a memory leak in long running daemon type apps...


glennj: When working with pipes (i.e. [set pipe [open "| some command]]]), and the pipe is blocking (i.e. [fconfigure $pipe -blocking yes] which is the default), then [close $pipe] throws an error if the command exited with non-zero status or if the command sent any output to stderr.

example:

 set command {sh -c {echo "to stdout"; echo >&2 "to stderr"; exit 42}}
 set pipe [open "| $command"]
 set standard_output [read -nonewline $pipe]
 set exit_status 0
 if {[catch {close $pipe} standard_error] != 0} {
     global errorCode
     if {"CHILDSTATUS" == [lindex $errorCode 0]} {
         set exit_status [lindex $errorCode 2]
     }
 }
 puts "exit status is $exit_status"
 puts "captured standard output: {$standard_output}"
 puts "captured standard error: {$standard_error}"

Output is:

 exit status is 42
 captured standard output: {to stdout}
 captured standard error: {to stderr}

See also channel, exec, stderr.


Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command