acapola
acapola

Reputation: 1243

TCL namespace and source

I have two files: main.tcl and paths.tcl.
main.tcl:

set p "paths.tcl"
puts $p
if {[lindex $argv 0]} {
    source $::p
    puts $::techDir
}
namespace eval ::newWkSpace {
    source $::p
}
puts $::newWkSpace::techDir

paths.tcl:

set techDir ../tech

And I do two trials:

$ tclsh main.tcl 0
paths.tcl
../tech
$ tclsh main.tcl 1
paths.tcl
../tech
can't read "::newWkSpace::techDir": no such variable
    while executing
"puts $::newWkSpace::techDir"
    (file "main.tcl" line 11)

Can someone explain the error, and why it does not happen in the first trial ? (that's with tcl 8.5)

Upvotes: 0

Views: 1399

Answers (1)

nurdglaw
nurdglaw

Reputation: 2117

I guess that in your second test case, when the interetter executed

namespace eval ::newWkSpace {
    source $::p
}

and came to execute

set techDir ../tech

it noticed that ::techDir existed, it assigned into that and didn't bother the create a techDir variable in the newWkSpace namespace.

A question that arises is, "Why are you doing this?". If you really want to get it working, try putting a variable techDir statement in your namespace before you source paths.tcl, thus:

namespace eval newWkSpace {
    variable techDir;   #<<<<<<<<<<<
    source $::p
}

I expect that someone who really understands Tcl will be along in a while to put me right and give you a proper answer.

Upvotes: 3

Related Questions