Programmer
Programmer

Reputation: 8727

Tcl How to pass arguments to a proc

I am new to TCL and having tough times to call a 3rd part proc which works perfectly if we pass values like below:

set result [3RD_par_api {{ifAdminStatus.2 Integer 1}}]
puts $result

Where ifAdminStatus.2 is an OID [SNMP Object Identifier] and Integer is datatype and value to be set is 1.

The code works fine but when I try to do using my script variables:

set result [3RD_par_api {{$id $data $val}}]
puts $result

I get error -

"illegal binding ...$id $data $val"

How can I pass user defined / variables as arguments to 3RD_par_api method and what does argument in double braces means "{{ }}"?

Upvotes: 2

Views: 926

Answers (1)

Brad Lanam
Brad Lanam

Reputation: 5763

Let's take a look at the differences. With a single set a braces, Tcl treats the argument as a single word (in some ways similar to single quotes in other languages). But if you access it as a list, it gets broken up into multiple pieces:

foreach elem {ifAdminStatus.2 Integer 1} {
   puts $elem
}

ifAdminStatus.2
Integer
1

And with doubled braces:

foreach elem {{ifAdminStatus.2 Integer 1}} {
   puts $elem
}

ifAdminStatus.2 Integer 1

The list now contains a single argument: "ifAdminStatus.2 Integer 1".

To create a list, use the list command:

# this is the same as {ifAdminStatus.2 Integer 1}
# it contains three strings
set arg [list ifAdminStatus.2 Integer 1]

# this is the same as {{ifAdminStatus.2 Integer 1}}
# it contains a single string
set arg [list {ifAdminStatus.2 Integer 1}]

Apparently the API wants a list with a single string as an argument. To create this with variables, use the list command twice.

set id ifAdminStatus.2
set data Integer
set val 1
set arg [list [list $id $data $val]]
foreach elem $arg {
   puts $elem
}

ifAdminStatus.2 Integer 1

What you want to try is:

set result [3RD_par_api [list [list $id $data $val]]]

Or simply:

set result [3RD_par_api [list "$id $data $val"]]

Upvotes: 1

Related Questions