Re: now do you "link" .tcl files?
- From: Konstantin Khomoutov <khomoutov@xxxxxxxxx>
- Date: Thu, 17 Sep 2009 08:44:09 -0700 (PDT)
On 15 сен, 21:19, Mark_Galeck <mark_galeck_spam_mag...@xxxxxxxxx>
wrote:
OK, thank you all, your explanations certainly helped me understandI think the best way to understand this topic is to stop thinking
what is going on and find a method that suits me.
I think from the position of a newbie, this is how linking in Tcl
should be explained (I don't see it explained this way anywhere).
Please correct if I am saying something stoopid.
about linking completely.
Let me repeat: when you have
foo bar
in your script, Tcl parses this as a request to call the command named
"foo" and hand it the string "bar" as its sole argment.
At the time of the call, Tcl looks up the command named "foo" using
the rules explained in the "namespace" manual page. If the name
exists, the command is executed, otherwise execution fails (in fact
the "execution fails" case is more complicated, but let's not touch it
right now).
Hence, there's no such thing as linking: the command is checked for
existence each time Tcl attempts to invoke it; demonstration:
% proc foo args { puts $args }
% foo a b c
a b c
% rename foo bar
% foo a b c
invalid command name "foo"
% rename bar foo
% foo a b c
a b c
As you can see, a command can appear or disappear any time during the
execution of the program.
Therefore, what you call "linking" is just *creation of the necessary
commands in the current interpreter*, and the availability of a
command is determined each time a call is made to that command. Please
try to make this statement sink in :-)
For instance, pkgIndex.tcl for a Tcl-only package usually contains
something like this:
package ifneeded fooPackage 1.0 \
"source [list [file join $dir fooPackage.tcl]]"
which basically means: "to load the package fooPackage source the file
fooPackage.tcl in the directory the pkgIndex.tcl being read is
located".
So, when you do "package require fooPackage" the interpreter will just
source the said Tcl script which presumably contains, among other
things, a series of calls to [proc] which will create their procedures
in the interpreter.
"Linking" implies you have a table of "unbound" symbols and
"something" which exports certain symbols. The linker then binds
unbound symbols to available exported symbols. Tcl, conversely, does
not bind any names until the actual call or a variable access is made;
this is a ultra-lazy language.
Again, when you [package require] a pure-Tcl package, the end result
will be no different from sourcing its files (using the [source]
command) in right order; the sourced files are *executed* by Tcl and
*create* procedures constituting the package *at runtime*.
1. To "declare" a .tcl file with some proc's as part of a package
("library") foobar version 1.0 use this command at the end of the
file:
package provide foobar 1.0
Quite correct, but it helps to understand that [package provide]
command has nothing to do with files. This call is made when the file
it appears in is sourced by the package loader, and after this call
the package manager knows the specified package was loaded.
The idea to put such a command at the end of a file is just to prevent
its execution when any of the commands above it fails, leaving the
package being loaded in an inconsistent state.
2. Then you need to let Tcl know about packages in a directory -No one requires you to run pkg_mkIndex -- just create pkgIndex.tcl by
execute command
pkg_mkIndex
This command will make a pkgIndex.tcl file in the directory with a
list of existing packages. Therefore, you need to do this once for
every directory that has packages declared, and redo it every time a
new package has been declared.
hand and put as many [package ifneeded] commands as you like.
If you decided to pick this way, making yourself understand how
pkgIndex.tcl machinery works is required. Please read info on [package
ifneeded] and [package require] in [1].
3. Then you need to let Tcl know which directories to search for
pkgIndex.tcl files. This is the auto_path built-in Tcl variable. The
env variable TCLLIBPATH is appended to auto_path - if you set it,
remember to use the syntax that Tcl expects, so for example
Windows>set TCLLIBPATH=C:\\tcl_lib
Correct, but better manipulate the auto_path variable directly from a
top-level script.
lappend ::auto_path [file dir [info script]]
is usually what you need.
4. Finally, to "link" to a package, use the command
package require ...
....to load the package, not link. Tcl has no linking.
1. http://www.tcl.tk/man/tcl8.5/TclCmd/package.htm
.
- Follow-Ups:
- Re: now do you "link" .tcl files?
- From: Don Porter
- Re: now do you "link" .tcl files?
- References:
- now do you "link" .tcl files?
- From: Mark_Galeck
- Re: now do you "link" .tcl files?
- From: Jeff Godfrey
- Re: now do you "link" .tcl files?
- From: Robert Heller
- Re: now do you "link" .tcl files?
- From: Mark_Galeck
- now do you "link" .tcl files?
- Prev by Date: Re: passing the result list of a command to a proc
- Next by Date: Re: Subtlety with creating lists of lists
- Previous by thread: Re: now do you "link" .tcl files?
- Next by thread: Re: now do you "link" .tcl files?
- Index(es):
Relevant Pages
|