Version 6 of package names

Updated 2005-10-15 09:32:07

package names

Returns a list of the names of all packages in the interpreter for which a version has been provided (via package provide) or for which a package ifneeded script is available. The order of elements in the list is arbitrary.


package names will only deliver all available packages if it has searched all package indices before. To force this, do a dummy package require before:

 catch {package require nonexistentName}
 package names

Lars H, 15 Oct 2005: Wouldn't package present be slightly better? If, against all odds, nonexistentName is the name of a package, we certainly don't want to load it.

DGP - To be more precise, [package names] returns a list of all the package names that are already known to the [package] command. That is not the same as all the names that could become known by operation of the [package unknown] callback.

The default [package unknown] callback is [tclPkgUnknown] and it is that default callback that exhibits the behavior described above -- after one run, all installed package names are known. The [package unknown] interface does not require that behavior, and other callbacks may not (IMHO, should not) implement it.

Lars H: Feeling slightly Cantorian, I'd propose the following for a foolproof method of listing all package names:

  proc all_package_names {} {
     while {![catch {package present "[package names]+1"}]} {}
     return [package names]
  }

The idea is that "[package names]+1" cannot be the name of a package known to the package database, so searching for it will force some additional data to be entered. It isn't until package present has raised an error that one can be sure the database has been fully loaded, however.


See also:


Tcl syntax help - Category Command - Category Introspection