Version 2 of safe.eval

Updated 2003-08-07 06:47:11

GPS: I'm really sick of the discussion over the expand vs. {} vs. {expand} and so on, so I came up with this. Comments are welcome...

 #Copyright 2003 George Peter Staplin
 #You may use this under the same terms as Tcl.
  proc safe.eval args {
  set expand 0
  set cmd [list]
  foreach arg $args {
   if {"~" == $arg} {
    set expand 1
   } else {
    if {$expand} {
     set cmd [concat $cmd $arg]
    } else {
     lappend cmd $arg
    }
    set expand 0
   }
  }
  uplevel 1 $cmd
 }

 proc A {a b c d e f g h} {
  puts "A:$a $b $c $d $e $f $g $h"
 }

 proc B {} {
  return [list 1 2 3]
 }

 proc main {} {
  set l [B]
  safe.eval A ~ $l "\"Hello \{\" World" ~ [list a b c] "Bye World"

  puts "Now testing with a typical Tk usage..."
  package require Tk
  pack [button .b]
  pack [button .b2]
  pack [button .b3]
  puts BEFORE:[winfo children .]
  safe.eval destroy ~ [winfo children .]
  puts AFTER:[winfo children .]
  exit
 }
 main