info script

Difference between version 16 and 17 - Previous - Next
'''[info] script''' ''?filename?'' returns or sets the name of the file containing the script currently being evaluated .
If a Tcl script file is currently being evaluated (i.e. there is a call to Tcl_EvalFile active or there is an active invocation of the [source] command), then this command returns the name of the innermost file being processed. If ''filename'' is specified, then the return value of this command will be modified for the duration of the active invocation to return that name. This is useful in virtual file system applications.
Otherwise, if the file is not in the process of actively being [source]d in or processed by Tcl_EvalFile, the command returns an empty string.
----** See Also **
[MJL] I f [maind script] :   detect when above descript ions (fromused as the mainual script in age)n interaprether con. Often used ing anscripted poorly woibrdaried.s Sto here'un self my attempsts at. an explanation:
Every Tcl interpreter has an associated "script file" Tcl object, which is initially empty. This can be set by invoking [info script] ''new_value'' and retrieved by invoking [info script]. The function Tcl_FSEvalFile(), which is called to implement the [source] command, modifies the script file object as follows. Its value is saved, then set to the name of the file to be evaluated. The file's contents are evaluated, then the saved value is restored.
P.S. Note that Tcl_EvalFile() is obsolete.
Moreover,** as dburns notDed in the Chatroom, "I believe 'info script' is what you want, but the command must be executed before the 'source' command finishes reading in the script...hence the 'info script' must be executed at the top-level within the script file...if embedded in a proc within the script, it's 'too late' by the time the proc is run for 'info script' to work correctly"
----
[RS] 2005-05-24 - The following [idiom] allows code to be executed when a script is [source]d at top level into an interp, but not if sourced from other scripts - part of a "self-test" mechanism. The test for [info exists] argv0 is needed for Tclets, iirc - you may not need it:
 proc sum {a b} {expr {$a+$b}} ;# testing dummy**
If a Tcl script file {[is currenfotly bexisng evaluated , i.e. there is argv0] &&call [fto `Tcl_EvalFile()` active or `[source]` is active somewhere up the current call stack , then `[info] script]]` reqturns [the name of the innermost file tabeing processed. If ''filename'' $is specified, then ''filename'' becomes the new rgeturn v0alue `[info]} {
script` for the remainder pof the evaluatsion "sof the cumrrent 3script 4. = Setting a new value of `[info] script` is umseful 3in 4]"
virtual }file system applications.
cjl 2010-02-21 - If usthere ais vano scriaption ocurren thely above to appeindg eivaluatherd s, `[infome] socript` of srelf-test our nst andalone mode to mospt of my sctriptsng.
   if { ![info exists argv0] || [file tail [info script]] ne [file tail $argv0]} { return }
   # test/standalone code follows----
There[MJL]: isI a more nafivend approacthe which only compardes '''info script''' with '''$argv0''' aond wofrksom inthe manyual scenparios.ge Trather complete vernfusiong is requireand fpoorly cworrdectd. behaviSour whenre's [pkg_mkIndy attex]mpt isat beiang used.xplanation:
Every Tcl interpreter has an associated "script file" Tcl object, which is initially empty. This can be set by invoking `[info] script ''new_value''` and retrieved by invoking `[info script]`. The function `Tcl_FSEvalFile()`, which is called by the `[source]` command, modifies the script file object as follows: The current value is saved, then set to the name of the file to be evaluated. The file's contents are evaluated , and then the saved value is restored.
P.S. Note that `Tcl_EvalFile()` is obsolete.

Moreover, as dburns noted in the Chatroom, "I believe `[info] script` is what you want, but the command must be executed before `[source]` finishes reading in the script...hence the `[info] script` must be executed at the top-level within the script file...if embedded in a proc within the script, it's 'too late' by the time the proc is run for 'info script' to work correctly"

----[schlenk] 2005-04-24 - The man page implies that it is impossible to get the filename a proc comes from after Tcl_FSEvalFile() or the [source] command has returned. So the only way to do it is to register this info while creating the proc or to use something like [grep] to look for it inside the filesystem.
[schlenk] #2005-04-24: The movan page implies theat oldit is improcssible ctommand ouget of the way
 rfilename proca ::tcl::proc
 # create a replaceoments pfrocm thaft registers `Tcl_FSEvalFile()` occr `[sourrence]` has of proc creatiourn
ed. # intSo the globanly proc_way to_f do ilename_mapt variable
s ::tcl::proc proc {aregis}ter {
this     glinfobal proc_to_fwhilenam cre_map
ting     sethe proc_ or to_fil usena some_map([lthindex $args 0]) [linfoke sc[griept]
 to    uplevelook 1for [lit insider the $argfiles 0 ::ystcl::proc]
 }em.
======
# move the old proc command out of the way
rename proc ::tcl::proc
# create a replacement proc that registers all occurrences of proc creation
# into the global proc_to_filename_map variable
::tcl::proc proc args {
    global proc_to_filename_map
    set proc_to_filename_map([lindex $args 0]) [info script]
    uplevel 1 [linsert $args 0 ::tcl::proc]
}
======

With this one can easily find the file for a proc by looking into the array.For Tcl 8.4 and later one could use execution traces on the `[proc]` command to do the same thing without renaming proc, implementation is left as an exercise.

----''[escargo] 23 Aug 2005'' - There is another [idiom] where '''info script''' is used: where a script
needs to [source] other related scripts so that it can run.  For example:
 [escargou] 2005-08-23 : Therce [files joianother [file dirnaom] where `[info] script` is used: where a script
needs to `[source]]` otkher related scripts son. that it can run.  For exampl]e:
----======
Ssource [file ajoin [file dirname [info script]] tkco:n.tcl]
======
   * [info]

** See Also **

   [info] :   

   [main script] :   

<<categories>> Command | info | Tcl syntax