• distinguis `tclsh mod-1.tm` vs `package require mod`

    From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Sun Dec 21 05:20:46 2025
    From Newsgroup: comp.lang.tcl

    I want to create a mod-1.tm file that contains some functions.
    However, I also want to include some tests.

    If I use:

    package require mod

    I don't want any tests run.

    But if I use:

    $ tclsh mod-1.tm

    or

    $ tclsh mod-1.tm arg1 arg2

    I want the tests to be run.

    How can I do this?

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From et99@et99@rocketship1.me to comp.lang.tcl on Sun Dec 21 13:04:04 2025
    From Newsgroup: comp.lang.tcl

    On 12/20/2025 9:20 PM, Mark Summerfield wrote:
    I want to create a mod-1.tm file that contains some functions.
    However, I also want to include some tests.

    If I use:

    package require mod

    I don't want any tests run.

    But if I use:

    $ tclsh mod-1.tm

    or

    $ tclsh mod-1.tm arg1 arg2

    I want the tests to be run.

    How can I do this?


    The global argv0 should be different, in the 2 tclsh cases, it should be mod-1.tm while if you are doing the package require mod, in some other file, run as say, tclsh myapp.tcl, then argv0 would be myapp.tcl.

    -e

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Emiliano@emiliano@example.invalid to comp.lang.tcl on Sun Dec 21 18:13:48 2025
    From Newsgroup: comp.lang.tcl

    On Sun, 21 Dec 2025 05:20:46 -0000 (UTC)
    Mark Summerfield <m.n.summerfield@gmail.com> wrote:

    I want to create a mod-1.tm file that contains some functions.
    However, I also want to include some tests.

    If I use:

    package require mod

    I don't want any tests run.

    But if I use:

    $ tclsh mod-1.tm

    or

    $ tclsh mod-1.tm arg1 arg2

    I want the tests to be run.

    How can I do this?

    See https://wiki.tcl-lang.org/page/main+script

    Regards
    --
    Emiliano <emiliano@example.invalid>
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Mark Summerfield@m.n.summerfield@gmail.com to comp.lang.tcl on Mon Dec 22 07:53:10 2025
    From Newsgroup: comp.lang.tcl

    On Sun, 21 Dec 2025 13:04:04 -0800, et99 wrote:

    On 12/20/2025 9:20 PM, Mark Summerfield wrote:
    I want to create a mod-1.tm file that contains some functions.
    However, I also want to include some tests.

    If I use:

    package require mod

    I don't want any tests run.

    But if I use:

    $ tclsh mod-1.tm

    or

    $ tclsh mod-1.tm arg1 arg2

    I want the tests to be run.

    How can I do this?


    The global argv0 should be different, in the 2 tclsh cases, it should be mod-1.tm while if you are doing the package require mod, in some other file, run as say, tclsh myapp.tcl, then argv0 would be myapp.tcl.

    -e

    Thank you and Emiliano for your answers. Both your approaches worked.
    In the end I've decided to use this:

    if {[string match *.tm $::argv0]} {
    # this executes if run directly; otherwise ignored
    }

    I never create my own pkgIndex packages only .tm modules so this is all I need since
    my applications always have a suffix of .tcl or .tk (or no suffix).
    --- Synchronet 3.21a-Linux NewsLink 1.2