NEM 26 Aug 2006: C# has a nice little construct for automatically closing a resource once you are finished with it. Here's a Tcl version of this using command:
proc using {varName chan script} { upvar 1 $varName var set var $chan set rc [catch { uplevel 1 $script } result] catch { close $chan } return -code $rc $result }
Which you can then use like:
using fd [open somefile.txt] { puts [read $fd] }
or
foreach host $hosts { using sock [socket $host $port] { ... do stuff on $host ... } }
[ Category Control Structure | Category File | Category Networking ]