获取TCL中执行代码的行号

获取TCL中执行代码的行号

本文介绍了获取TCL中执行代码的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印执行TCL脚本的行号?

how to print the line number of executing TCL script?

#! /usr/bin/tclsh

set a "100"
set b "200"
set c [expr $a + $b]
puts [info script] ;# it will display the script name which it is executing.
                    # similarly I need to print the script line number.
puts $c

推荐答案

你必须使用 info frame 来简单地完成这项工作.

You have to use info frame to get this done simply.

info frame ?number?

此命令提供对堆栈上所有帧的访问,甚至是那些在信息级别隐藏的帧.如果未指定 number,则此命令返回一个数字,给出命令的帧级别. 如果在顶层调用该命令,则为 1.如果指定了数字,则结果是一个字典,其中包含堆栈中编号级别的命令的位置信息.

This command provides access to all frames on the stack, even those hidden from info level. If number is not specified, this command returns a number giving the frame level of the command. This is 1 if the command is invoked at top-level. If number is specified, then the result is a dictionary containing the location information for the command at the numbered level on the stack.

如果 number 是正数 (> 0) 则它选择一个特定的堆栈级别(1 指最顶层的活动命令,即 info frame 本身,2 指从中调用它的命令, 等等);否则,它会给出与当前命令相关的级别(0 表示当前命令,即信息帧本身,-1 表示其调用者,依此类推).

If number is positive (> 0) then it selects a particular stack level (1 refers to the top-most active command, i.e., info frame itself, 2 to the command it was called from, and so on); otherwise it gives a level relative to the current command (0 refers to the current command, i.e., info frame itself, -1 to its caller, and so on).

我们将使用 info frame 命令返回的字典.其中一个键是'line',它包含脚本的行号.

We are going to make use of the dictionary returned by the info frame command. One of the key is 'line' which contains the line number of the script.

有一个简单的 proc 作为,

Have a simple proc as,

proc printLine {frame_info} {
    # Getting value of the key 'line' from the dictionary
    # returned by 'info frame'
    set result [dict get [info frame $frame_info]  line]
}

通常,[info frame $frame_info] 生成的字典类似于,

In general, the resulting dictionary from [info frame $frame_info] will be something like,

type source line 17 file/home/dinesh/stackoverflow/test cmd {printLine [info frame] } proc ::B level 1

由此,我们只是通过 dict get

只需使用该上下文的当前帧号调用此过程,这可以通过 info frame 本身实现.

Just call this proc with the current frame number of that context which can be achieved with info frame itself.

set lineNumber [printLine [info frame]]; #Place this line in your code.

此逻辑的演示如下.

printLineNumber.tcl

#!/usr/bin/tclsh
proc printLine {frame_info} {
        # Getting value of the key 'line' from the dictionary
        # returned by 'info frame'
        set result [dict get [info frame $frame_info]  line]
}
proc D {} {
        puts "proc D"
        puts [ printLine [info frame] ]
}
proc C {} {
        puts "proc C"
        puts [ printLine [info frame] ]
        D
}
proc B {} {
        puts "proc B"
        puts [ printLine [info frame] ]
        C
}
proc A {} {
        puts "proc A"
        puts [ printLine [info frame] ]
        B
}

puts "Global"
puts [ printLine [info frame] ]
A

文档:信息dict

这篇关于获取TCL中执行代码的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:31