2014-09-22 59 views
0

我想在tcl中创建一个跟踪函数。函数将列出所有被调用的procs/nested调用和那里的参数。下面是脚本无法放入tcl脚本

rename proc _proc 
proc proc {nm params body} { 
    _proc $nm $params $body 
    trace add execution $nm enter [list track_entry $nm $params] 
    trace add execution $nm leave [list track_leave $nm] 
} 
_proc track_entry {nm params real args} { 
    puts "Enter proc $nm" 
    foreach formal $params actual [lrange $real 1 end] { 
     append p " [lindex $formal 0]=$actual," 
    } 
    puts "Parameters:$p and body" 
} 
_proc track_leave {nm args} { 
    puts "Exit proc $nm" 
} 
proc test1 { param1 param2 } { 
    puts “parameters are $param1 and $param2” 
    test2 value1 
} 
proc test2 { value} { 
    puts “value is $value” 
} 

我得到以下输出

test1 arg1 arg2 

Enter proc test1 
Parameters: param1=arg1, param2=arg2, and body 
Exit proc test1 
wrong # args: should be "puts ?-nonewline? ?channelId? string" 

任何线索,为什么它是在puts

+4

为什么你在最后两个'proc's引号(''“'')而不是简单引号('”“')中引用了? – Jerry 2014-09-22 07:38:45

回答

3

前提是你贴什么是正确的给错误,问题是你不使用正确的引号字符。

的Tcl既懂2种引用的:

  1. 具有取代报价:""
  2. 没有取代的报价:{}

字符在TCL将仅仅被视为任何其他字符如a5

请注意,如果没有引用,tcl会将该单词视为没有空格的字符串。例如,下面的例子都是有效的字符串:

this_is_a_valid_string 
"this_is_a_valid_string" 
{this_is_a_valid_string} 

遵循这个简单的规则,下面也是有效的字符串,都是等价的:

“hello 
"“hello" 
{“hello} 

所以,当你问TCL执行以下:

puts “parameters are $param1 and $param2” 

它把它作为:

puts {“parameters} {are} "$param1" {and} "$param2”" 

将5个参数传递给puts

显然这会触发一个错误,因为puts需要一个或两个参数。