Posted to tcl by aspect at Sun Jul 13 04:54:30 GMT 2008view pretty

## .. trying to achieve [uplevel -except {somevars}
## use [testme] as an example caller.
# The first [uplevel] call below is what I currently
# need to do, which I find a bit ugle. The last is
# what I want and the second (commented) is the effect
# I'm currently obtaining, except [apply] doesn't
# support lexical scope for its lambdas.

# I guess this isn't a very tclish thing to do, but
# I do have legitamate uses for it in Expect and Xchat.
# How would a Real Tcler approach this problem?

  proc log {args} {
    set caller [lindex [info level -1] 0]
    uplevel 1 "
      if {\$_verbose} {
        puts {LOG: \[$caller\] $args}
      } else {
        puts {LOG: $args}
      }
    "
#    uplevel 1 {apply {{caller args} {
#      if {$_verbose} {
#        puts "LOG: \[$caller\] $args"
#      } else {
#        puts "LOG: $args"
#      }
#    }} {testme {with verbose = 1}}}
    uplevel 1 -except {caller args} {
      if {$_verbose} {
        puts "LOG: \[$caller\] $args"
      } else {
        puts "LOG: $args"
      }
    }
  }

  proc testme {} {
    set _verbose 1
    log with verbose = $_verbose
    set _verbose 0
    log with verbose = $_verbose
  }