* Mark <m.n.sum...@googlemail.com>
| Suppose I have a project:
| app.tcl
| liba.tcl
| libb.tcl
| and in app.tcl I use functions from liba.tcl and libb.tcl (in
| namespaces liba:: and libb:: respectively).
| Is there a standard recommended way to import these files?
I think I would make liba and libb packages:
- liba.tcl
package provide liba 1.0
- libb.tcl
package provide libb 1.0
- new file: pkgIndex.tcl
package ifneeded liba 1.0 [list source -encoding utf-8 [file join $dir liba.tcl]]
package ifneeded libb 1.0 [list source -encoding utf-8 [file join $dir libb.tcl]]
- app.tcl
# optional (if not installed in auto_path)
lappend auto_path [file dirname [info script]]
# load liba and libb
package require liba
package require libb
If 'liba' and 'libb' are not only short versions for demo purposes,
maybe it would be a good idea to name the packages with some prefix (app_liba etc), since plain 'liba' and 'libb' are rather generic...
HTH
R'
Anyway, here's the structure I'm using and which seems to work based
on your advice.
And include a "package provide" with the same version number inside.
On 01/06/2023 15:07, Rich wrote:
And include a "package provide" with the same version number inside.
That would be redundant, because the version number is already
specified in the file name and Tcl will pick it up from there. So
there is no need to include a "package provide" command in a module
code.
I read the docs for tm and they say "A Tcl Module is a Tcl Package contained in a single file, and no other files required by it". This seems a bit restrictive compared to the package approach. And anyway, I'm only really looking at how to spread an application over multiple files; all the tcl files are for the application, they're not self-contained as such.
I've now tried using modules and despite the docs it seems to work fine and requires one less file (no need for pkgIndex.tcl)
# file: myapp.tcl
tcl::tm::path add [file dirname [info script]]
package require app
app::main
# file: app-1.0.0.tm
package provide app 1.0
package require util
namespace eval app {}
proc app::main {} {
puts "app.tcl main [util::commas 1234567890]"
}
# file: util-1.0.0.tm
package provide util 1.0
namespace eval util {}
proc util::commas {x} {
regsub -all \\d(?=(\\d{3})+([regexp -inline {\.\d*$} $x]$)) $x {\0,}
}
Although the above works fine, it causes nagelfar to complain:
$ nagelfar.tcl -quiet -H -tab 4 -len 76 -Wunusedvar *.{tcl,tm}
myapp.tcl: 8: W Unknown command "app::main"
app-1.0.0.tm: 9: W Unknown command "util::commas"
I read the docs for tm and they say "A Tcl Module is a Tcl Package contained in a single file, and no other files required by it". This seems a bit restrictive compared to the package approach. And anyway, I'm only really looking at how to spread anapplication over multiple files; all the tcl files are for the application, they're not self-contained as such.
El jueves, 1 de junio de 2023 a las 16:28:02 UTC+2, Mark escribió:
I read the docs for tm and they say "A Tcl Module is a Tcl Package
contained in a single file, and no other files required by it".
This seems a bit restrictive compared to the package approach. And
anyway, I'm only really looking at how to spread an application over
multiple files; all the tcl files are for the application, they're
not self-contained as such.
so what's the real difference between a package and a module in terms
of use?
it seems modules are just an evolution over packages intended to the
same use.
After reading the replies in this thread (and without reading any
doc), I have a doubt, if package provide is not required in a module,
how tcl knows what names you want to export in the module?
is it the case that all symbols defined in the module are public by
default (and thus imported when you do a package require)?
El jueves, 1 de junio de 2023 a las 16:28:02 UTC+2, Mark escribió:application over multiple files; all the tcl files are for the application, they're not self-contained as such.
I read the docs for tm and they say "A Tcl Module is a Tcl Package contained in a single file, and no other files required by it". This seems a bit restrictive compared to the package approach. And anyway, I'm only really looking at how to spread an
so what's the real difference between a package and a module in terms of use? it seems modules are just an evolution over packages intended to the same use.are public by default (and thus imported when you do a package require)?
After reading the replies in this thread (and without reading any doc), I have a doubt, if package provide is not required in a module, how tcl knows what names you want to export in the module? is it the case that all symbols defined in the module
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 463 |
Nodes: | 16 (2 / 14) |
Uptime: | 157:02:57 |
Calls: | 9,384 |
Calls today: | 4 |
Files: | 13,561 |
Messages: | 6,095,919 |