• Sourcing another Tcl file

    From snosniv@21:1/5 to All on Mon Nov 7 05:38:48 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to snosniv on Mon Nov 7 13:57:11 2022
    snosniv <nivparsons@gmail.com> wrote:
    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.

    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]

    ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Mon Nov 7 15:02:32 2022
    Am 07.11.2022 um 14:38 schrieb snosniv:
    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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From snosniv@21:1/5 to Harald Oehlmann on Mon Nov 7 06:18:23 2022
    On Monday, 7 November 2022 at 14:02:54 UTC, Harald Oehlmann wrote:
    Am 07.11.2022 um 14:38 schrieb snosniv:
    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.
    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

    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Harald Oehlmann@21:1/5 to All on Mon Nov 7 16:25:29 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From et4@21:1/5 to snosniv on Mon Nov 7 12:39:08 2022
    On 11/7/2022 5:38 AM, snosniv wrote:
    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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mole Cool@21:1/5 to All on Wed Nov 9 18:11:39 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)