0

I'm pretty much a newb, and I'm trying to set some variables in a namespace which use argv, and then to call them from a proc outside of the namespace, but I'm having trouble understanding how to do this. I'm trying to use some code like this (but clearly this is the wrong way to do it):

namespace eval Ns {
    variable spec [lindex $argv 1]
}

proc p {} {
    set spec "::Ns::spec"

}
4

2 回答 2

1

The correct way is to use variable:

proc p {} {
    variable ::Ns::spec
    # ...
}

Also possible is upvar:

proc p {} {
    upvar #0 ::Ns::spec spec
    # ...
}

or (almost) like you did it:

proc p {} {
   set spec $::Ns::spec
   # ...
}

This last possibility will not change the variable if it is changed in the proc.

于 2013-03-29T22:22:38.617 回答
0

...Or make a call to return the value:

proc Ns::getspec { } {
    variable spec

    return spec;
}
于 2013-04-02T13:51:13.093 回答