1
votes

I have a "library" type file that where I define a namespace and export some functions to "print" debug info or "trace" info in TCL.

proc cputs  { args } {
    puts "Test= $args"
}

I import this function in multiple other files. When I use it everything is fine except it add bracket to the output.

Like if I call cputs "Hi there" it will output {Hi there} instead of Hi there.

Can't find anything to explain that. I would like to get ride of those brackets.

Any help would be appreciated.

Thanks

2

2 Answers

2
votes

If you do not want to pass an arbitrary, unknown numbers of arguments to your cproc, do not use args (which has a special meaning as a parameter name), but msg, for instance.

proc cputs  {msg} {
    puts "Test= $msg"
}

See also proc documentation:

If the last formal argument has the name “args”, then a call to the procedure may contain more actual arguments than the procedure has formal arguments. In this case, all of the actual arguments starting at the one that would be assigned to args are combined into a list (as if the list command had been used); this combined value is assigned to the local variable args.

1
votes

If you want to take an arbitrary number of arguments (since args is a special name), you should join or concat things before printing. These two options differ in their treatment of extra whitespace; use the one that suits you.

proc cputs { args } {
    puts "Test= [join $args]"
}

cputs "Hello there   " "   from an example"
# Test= Hello there      from an example 
proc cputs { args } {
    puts "Test= [concat {*}$args]"
}

cputs "Hello there   " "   from an example"
# Test= Hello there from an example