Updated 2015-09-04 21:31:57 by RLH

Also known as RLH

Email

set email [string map {at @ x . ext com} rlhicksatwehicksxext]

LinkedIn

My Public Profile

To do list

Books I need to get

  • Tcl and Tk Programming for the Absolute Beginner
  • Tcl and the Tk Toolkit
  • Tcl 8.5 Network Programming [x]
  • Tcl and Tk 8 Programming Cookbook
  • incr Tcl from the Ground Up

Things I like

  • I love the Tcl community
  • I like that Tcl is different from other dynamic languages syntax wise

This is just a simple attempt at adding a 'with' command like other language such as Python have. As I find other utility items I will add them to the package.
# futils.tcl --
#
#   This package provides some extra file utilities that are not in the core or
#   in tcllib.
#

package provide futils 1.0

#---------------------------------------------
# Create the namespace
#---------------------------------------------
namespace eval ::futils {
    # export the commands
    namespace export with
}

# ::futils::with --
#
#       This proc ensures that files get closed even when errors happen. It is
#       inspired by the Python language with function.
#
# Arguments:
#       file    file to open
#
# Examples:
#       ::with [open foo.txt w] as file { 
#           puts $file "Hello!" 
#       }
#
#       ::with [open foo.txt r] as file {
#           set data [read $file]
#           puts $data
#       }
#
proc ::futils::with {file as name block} {
   uplevel 1 [list set $name $file]

   if {[catch {uplevel 1 [list eval $block]} result]} {
       puts stderr "Warning: $result"
   }
   
   uplevel 1 [list close $file]
}