从包含的文件中获取“呼叫者”文件名

时间:2019-12-12 19:02:11

标签: tcl

我有很多不同的脚本,其中包括另一个脚本(我们可以将其宽松地称为一个库),并且我试图弄清楚如何(从包含的脚本中)获取包含该脚本的脚本的名称。问题是“信息脚本”命令返回JasperGold(工具名称),而不是我在工具上运行的脚本名称。知道怎么做吗?

A.tcl:

source B.tcl
…

B.tcl

# Some command that will return “A.tcl”
…

2 个答案:

答案 0 :(得分:2)

您是否按照Donal的建议尝试使用info frame?对于一个基本情况,这对我有用:

# B.tcl (top level)
dict get [info frame 1] file

答案 1 :(得分:2)

更多充实的例子:

$ cat -n a.tcl
     1  puts "this is [info script]"
     2  source b.tcl
$ cat -n b.tcl
     1  proc main {} {
     2      puts "this is [info script]"
     3      c
     4  }
     5  proc c {} { d }
     6  proc d {} {
     7      set n [info frame]
     8      puts "absolute frames"
     9      for {set i $n} {$i > 0} {incr i -1} {
    10          puts [list $i [info frame $i]]
    11      }
    12      puts "relative frames"
    13      for {set i 0} {$i > -$n} {incr i -1} {
    14          puts [list $i [info frame $i]]
    15      }
    16  }
    17
    18  main
$ tclsh a.tcl
this is a.tcl
this is b.tcl
absolute frames
5 {type source line 10 file /Users/glennjackman/tmp/b.tcl cmd {info frame $i} proc ::d level 0}
4 {type source line 5 file /Users/glennjackman/tmp/b.tcl cmd {d } proc ::c level 1}
3 {type source line 3 file /Users/glennjackman/tmp/b.tcl cmd {c } proc ::main level 2}
2 {type source line 18 file /Users/glennjackman/tmp/b.tcl cmd main level 3}
1 {type source line 2 file /Users/glennjackman/tmp/a.tcl cmd {source b.tcl} level 3}
relative frames
0 {type source line 14 file /Users/glennjackman/tmp/b.tcl cmd {info frame $i} proc ::d level 0}
-1 {type source line 5 file /Users/glennjackman/tmp/b.tcl cmd {d } proc ::c level 1}
-2 {type source line 3 file /Users/glennjackman/tmp/b.tcl cmd {c } proc ::main level 2}
-3 {type source line 18 file /Users/glennjackman/tmp/b.tcl cmd main level 3}
-4 {type source line 2 file /Users/glennjackman/tmp/a.tcl cmd {source b.tcl} level 3}