从包含的文件中获取 "caller" 文件名
get "caller" filename from within included file
我有一堆不同的脚本,其中包含另一个脚本(我们可以笼统地称它为库),我试图弄清楚如何(从包含的脚本中)获取包含它的脚本的名称。问题是“信息脚本”命令 returns JasperGold(工具名称)而不是脚本名称我是 运行 on 工具。知道怎么做吗?
A.tcl:
source B.tcl
…
B.tcl
# Some command that will return “A.tcl”
…
您是否按照 Donal 的建议尝试使用 info frame
?对于基本情况,这对我有用:
# B.tcl (top level)
dict get [info frame 1] file
更具体一点的例子:
$ 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}
我有一堆不同的脚本,其中包含另一个脚本(我们可以笼统地称它为库),我试图弄清楚如何(从包含的脚本中)获取包含它的脚本的名称。问题是“信息脚本”命令 returns JasperGold(工具名称)而不是脚本名称我是 运行 on 工具。知道怎么做吗?
A.tcl:
source B.tcl
…
B.tcl
# Some command that will return “A.tcl”
…
您是否按照 Donal 的建议尝试使用 info frame
?对于基本情况,这对我有用:
# B.tcl (top level)
dict get [info frame 1] file
更具体一点的例子:
$ 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}