0

Using tcl, i want to pass variable parameters to function. i tried this code

proc launch_proc { msg proc_name {params {}} } {
    puts "params launch_proc is $params \n"
}
proc test { param } {       
    puts "param test is $param \n"
    launch_proc "1.5.2  test param" test_standard {{*}$param param1 param2 param3"
    }       
}
test value

--> params launch_proc is {*}$param param1 param2 param3" $param is not evaluated (i use tcl 8.5)

4
  • You can pass only 3 arguments to launch_proc, but you are passing more than (incorrect ?) that. Also, issues with the quoting as well. Commented Dec 15, 2015 at 11:49
  • You are passing the string {*}$param param1 param2 param3 to launch_proc. It does not look like what you want. Did you mean launch_proc "1.5.2 test param" test_standard "{*}$param param1 param2 param3"? Commented Dec 15, 2015 at 13:02
  • Or better maybe launch_proc "1.5.2 test param" test_standard [list {*}$param param1 param2 param3] Commented Dec 15, 2015 at 13:04
  • Does the variable $value contain any value when you invoke the test proc? Commented Dec 15, 2015 at 14:15

3 Answers 3

2

Tcl has support for this using the keyword args as the last parameter in the argument list.

Here is an example directly from the wiki:

proc demo {first {second "none"} args} {
    puts "first = $first"
    puts "second = $second"
    puts "args = $args"
}

demo one
demo one two
demo one two three four five

results in

first = one
second = none
args = 
first = one
second = two
args = 
first = one
second = two
args = three four five

You can use the expand syntax for this as well which makes the following two calls to demo equivalent.

demo one two three four five
set params {three four five}
demo one two {*}$params
Sign up to request clarification or add additional context in comments.

Comments

1

You're passing a list and need to instead send each item as a parameter to the proc.

proc test {p1 p2 p3} {
        puts "$p1 - $p2 - $p3"
}

set value {one two three}

# This should work in tcl 8.5+
test {*}$value

set value {four five six}

# For tcl < 8.5
foreach {p1 p2 p3} $value {
        test $p1 $p2 $p3
        break
}

# or

set value {seven eight nine}

test [lindex $value 0] [lindex $value 1] [lindex $value 2]

Output:

$ tclsh test.tcl
one - two - three
four - five - six
seven - eight - nine

Comments

0

You need upvar, and use list when you construct the params list for launch_proc

proc test {varname} {
    upvar 1 $varname value
    puts "param test is $varname => $value"
    launch_proc "1.5.2 test param" test_standard [list {*}$value par1 par2 par3]
}

proc launch_proc {msg proc_name {params {}}} {
    puts "params launch_proc: [join $params ,]"
}

set value {"a b c" "d e f" "g h i"}
test value
param test is value => "a b c" "d e f" "g h i"
params launch_proc: a b c,d e f,g h i,par1,par2,par3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.