在 tcl 8.4 中获取行号

Getting line number in tcl 8.4

我需要在它的主体中获取 tcl proc 的调用行号。

从 8.5 开始,tcl 具有允许以下内容的信息帧命令:

proc printLine {} {
    set lineNum [dict get [info frame 1]  line]
}

8.4 需要同样的东西

在 8.4 中不可用;根本没有收集数据。我想您可以在该行中搜索唯一标记,但仅此而已。

proc lineNumber {uniqueToken} {
    set name [lindex [info level 1] 0]
    set body [uplevel 2 [list info body $name]]
    set num 0
    foreach line [split $body \n] {
        incr num
        if {[string first $uniqueToken $line] >= 0} {
            return $num
        }
    }
    error "could not find token '$uniqueToken'"
}

请注意,不再支持 8.4。升级。

我使用的是 tcl 8.5,但它应该适用于 8.4 版。这里是:

#!/usr/bin/tclsh

puts "tcl version: $tcl_version"

proc linum {} {
    if {![string equal -nocase precompiled [lindex [info frame -1] 1]]} {
        return [lindex [info frame -1] 3]
    } else {
        return Unknown
    }
}

puts "call proc @line:[linum]"

结果是:

tcl version: 8.5
call proc @line:13

您可以参考info frame了解更多详情