1
votes

Is there a way(a method call) to find out the name of the current package in tcl??

Eg:

package provides abc

proc A {

// I need to print the package name abc. 

}

I know the class name here is obviously "abc" but still I want to print it out using a tcl command. I'm working on some debug modules and hence need this. (Similar to what perl provides: __PACKAGE__)

2
No. You can provide several different packages in the same file. info script, info level and info frame could help, but they don't provide the package name. - Johannes Kuhn

2 Answers

1
votes

I am not aware of anything like that. However, you can work around:

set __PACKAGE__ foo
package provide $__PACKAGE__ 1.0
# Use can use the variable $__PACKAGE__ from now on
0
votes

You are missing a few pieces of the puzzle. Even though you declared proc A in a file that is part of a package, A is still created globally.

The package command really just helps tcl figure out which file to source. You usually want to mix package provide with namespace

package provide abc

namespace eval ::abc {
    proc A {} {puts stdout "I am in namespace [namespace current]"}
    proc B {} {..}
    proc C {} {..}
}

In order to call this function you would say

::abc::A

From inside the body of A you can tell what namespace you are in by using namespace current

An alternate way of writing this would be

namespace eval ::abc {}
proc ::abc::A {} {puts stdout "I am in namespace [namespace current]"}
proc ::abc::B {} {..}
proc ::abc::C {} {..}

There is not a one to one mapping between packages and namespaces. So one package could create many namespaces (or like in your example, no namespaces).

Check out this page for how to build libraries: http://www.tcl.tk/man/tcl8.5/tutorial/Tcl31.html

And this page for full instructions on package and namespace http://www.tcl.tk/man/tcl8.6/TclCmd/contents.htm