Of late, I've been using this proc a lot to deal with variable numbers
of arguments that may have different meanings.
proc > {args} {
upvar args argl
set j 0
foreach arg $args {
set val [lindex $argl $j]
if {[string index $val 0] eq "&"} {
set val [string range $val 1 end]
uplevel upvar $val $arg
} else {
upvar $arg local
set local $val
}
incr j
}
}Suppose you have:
proc test args {> a b c; puts "a=$a, b=$b, c=$c"}You have simple, positional parameters. The equivalent of:
proc test {a b c} {puts "a=$a, b=$b, c=$c"}But you don't have to require
just those 3 in that order. You can pick off the first one:
> a
and use that to decide what other arguments you are expecting.
proc test args {
> op
switch $op {
this {> b c; ...}
that {> b c d e f; ...}
other {...}
}
}One other convenience, it will automatically recognize and deal with var parameters
set var foo
proc test args {
> a b c
set c "foobar"
}
The call of
test 1 2 &varWill change var as expected.