3
votes

i am a newbie in TCL Programming

I am having a tcl script called test1.tcl and test2.tcl separately in two different

directories F:\TCLPrograms\SamplePrograms\test1.tcl and F:\TCLPrograms\test2.tcl

i want to know the full path of test2.tcl which is a proc

if i give info [script] inside proc disp {} its returning the path from where it is invoked

i.e F:\TCLPrograms\SamplePrograms\test1.tcl

kindly someone tell me to get the path of the proc

test1.tcl:

puts "Processing test1..."
source "F:\\TCLPrograms\\test2.tcl"
set rc [disp]
puts "Executed...."

test2.tcl:

proc disp { } {
puts "Successfully executed test2.tcl"
set path [info script]
puts "Script is invoked from the path: $path"
}

Thanks in advance

1

1 Answers

6
votes

The result of info script depends on the current innermost source, and procedures don't maintain that information. (Well, it's maintained in debugging information for 8.6 and some builds of 8.5 from ActiveState, but it's truly awkward to access.)

The easiest way is to use a variable to hold the name of the file, like this:

variable dispScriptFile [file normalize [info script]]
proc disp {} {
    variable dispScriptFile
    puts "Successfully executed test2.tcl"
    set path [file dirname $dispScriptFile]
    puts "Script is invoked from the path: $path"
}

Note that we use the normalized filename, so that it remains valid even if you use a relative pathname and then cd to some other directory. (I also recommend putting the whole contents of test2.tcl inside its own namespace; it makes it easier to keep things separate.)