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

  1. ## .. trying to achieve [uplevel -except {somevars}
  2. ## use [testme] as an example caller.
  3. # The first [uplevel] call below is what I currently
  4. # need to do, which I find a bit ugle. The last is
  5. # what I want and the second (commented) is the effect
  6. # I'm currently obtaining, except [apply] doesn't
  7. # support lexical scope for its lambdas.
  8.  
  9. # I guess this isn't a very tclish thing to do, but
  10. # I do have legitamate uses for it in Expect and Xchat.
  11. # How would a Real Tcler approach this problem?
  12.  
  13. proc log {args} {
  14. set caller [lindex [info level -1] 0]
  15. uplevel 1 "
  16. if {\$_verbose} {
  17. puts {LOG: \[$caller\] $args}
  18. } else {
  19. puts {LOG: $args}
  20. }
  21. "
  22. # uplevel 1 {apply {{caller args} {
  23. # if {$_verbose} {
  24. # puts "LOG: \[$caller\] $args"
  25. # } else {
  26. # puts "LOG: $args"
  27. # }
  28. # }} {testme {with verbose = 1}}}
  29. uplevel 1 -except {caller args} {
  30. if {$_verbose} {
  31. puts "LOG: \[$caller\] $args"
  32. } else {
  33. puts "LOG: $args"
  34. }
  35. }
  36. }
  37.  
  38. proc testme {} {
  39. set _verbose 1
  40. log with verbose = $_verbose
  41. set _verbose 0
  42. log with verbose = $_verbose
  43. }
  44.  
  45.