Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sourcing another Tcl file

442 views
Skip to first unread message

snosniv

unread,
Nov 7, 2022, 8:38:51 AM11/7/22
to
I have a set of files which break up my program into sections, such as GUI, tooltips, procedures etc.
The GUI interface is my main program from which I want to source the other Tcl programs, all in the same directory.
Obviously I can use: cd C:/dir/dir.... and then just use source <file>

BUT, I'd like to be able to do this from whatever directory the files all happen to be in, so I need a generic way of "cd-ing" to the dir wherever it is.

I thought something like set x [pwd]
then... cd $x would work? Apparently not.

Windows 10 PC if that makes a difference.

TIA, Kev P.

Rich

unread,
Nov 7, 2022, 8:57:15 AM11/7/22
to
If all the ancillary files are in the same directory as the main
script, you can use [file dirname], [file join] and [info script] to
find and source them.

[info script] gives you the location and name of the script. You then
need to use [file dirname] to extract just the path component. Then
file join that path to each script file name:

set srcpath [file dirname [info script]]

source [file join $srcpath aux1.tcl]
source [file join $srcpath aux2.tcl]

...

Harald Oehlmann

unread,
Nov 7, 2022, 9:02:54 AM11/7/22
to
Dear Key,

the normal way to organize this is to use packages, namespaces and a
pckIndex.tcl file.
Then, you put your folder with the pckIndex.tcl file into your auto_path
search list.
There is a couple of explanation required here and Ashoks book may help
to get the basics.

What I do:
- my program has a main folder, which is relative to the start script.
- Say, your start script is somewhere, and all other folders are in
subfolder "lib".
- So add in the start script:

lappend auto_path [file join [file dirname [info script]] lib]

This command will automatically cause a scan for pckIndex.tcl files in
the given folder and all direct sub-folders.

The pckIndex.tcl file has an entry for each package in the same folder,
like this:
-pckIndex.tcl---
package ifneeded tools 1.0 [list source -encoding utf-8\
[file join $dir tools.tcl]]
-EOF-
A package "tools" with Version 1.0 is in the file tools.tcl

-tools.tcl- has the following
package provide tools 1.0
% program code
-EOF-

And the start script does this to load the package:
package require tools

It is common, that the package tools will reside in the namespace "tools".

If you have any other packages (3rd party), just put them also in your
lib folder and get them by package require.

Sorry, this does not answer your question directly,
Harald

snosniv

unread,
Nov 7, 2022, 9:18:26 AM11/7/22
to
Thanks Harald,
I'm only a "Dabbler" in Tcl, not a serious programmer (I'm a retired electronic hardware designer), so simple solutions suit me best!

Harald Oehlmann

unread,
Nov 7, 2022, 10:25:34 AM11/7/22
to
> Thanks Harald,
> I'm only a "Dabbler" in Tcl, not a serious programmer (I'm a retired electronic hardware designer), so simple solutions suit me best!

Thanks for the feed-back. You will be happy with the solution by Rich.

The package way has a lot of advantages:
- using 3rd party packages is easy
- package dependencies are solved by "package require". They are only
loaded once, even if required by multiple packages.
- namespaces are useful to avoid variable and name overlaps.

Take care,
Harald

et4

unread,
Nov 7, 2022, 3:39:21 PM11/7/22
to
Since your files are each source-able, you could look at Tcl modules. If you want to place your files in a particular directory, say, c:/modules

Then you could use this statement:

tcl::tm::path add c:/modules

Then you give each of your files a specially formatted name, like say, mymodule-1.0.tm and then you can use

package require mymodule

Here, the 1.0 is the version number and the name must begin with the module name, a dash, and end with the extension .tm The package/module system will locate and source your file for you.


There's also a command

tcl::tm::path list

which will output a list of known module directories. You could pick one of those to put you files in, then you would only need the [package require] command.

And with all things tcl 8.6 the best resource is "The Tcl Programming Language" by Ashok. It's where I learned about modules.

et


Mole Cool

unread,
Nov 9, 2022, 9:11:41 PM11/9/22
to
For me, how to start is the most crucial part at all. I have everything in one sandbox, this sandbox can be moved into different folder or machines, but still works immediately. I use one folder where I start the wish executable with the first file to source within a batch file. In this batch file, you figure out where you are (what is my path). You also could setup some env vars before running wish. The first argument you give to the wish exe is known in your first script via argv0, or inside with ‘info script’. From the batch you know your full path, so you give this full path to your init script.

Each program I develop, has ONE app folder in the sandbox.

All my init scripts, calling at the end only one init proc. Here you have now your folder path. Next I use a var which contains all files, which are required for my program to run. You have the path and the source list, now you can source all this files, and after you have sourced, you call the ‘main’ procedure. This source list var is useful during debug and resource. All my source file having NO global statements, only procs and namespaces. This makes it than easy to resource during debug.The only global statement is the call to the init procedure. The init file looks the same for each main program, only the list of files which needs to be sourced on start and the main call MAY differ.

This is similar to having include files in C. And if you run the wish event loop you can update your code during develop on the fly, or even to release, you know all parts of your program. Yo may end up on release in one single file without any debug calls.

0 new messages