Namespaces
The use of namespaces is one area in which THT differs from other web scripting
environments. THT does not run scripts in
the global namespace.
Normally, code is evaluated in the ::main namespace
(although this can be changed with -cfg {defaultns ::myns}).
The builtin arrays ENV, THT, QUERY, COOKIES, and FILES also reside in this namespace.
The main implication of namespaces is that
user code is not prone to accidentally overwriting builtin commands.
The following code fragment demonstrates this:
<?tht
echo "<B>Hello world</B>"
proc echo str {
# user override.
}
# Access global echo.
::echo "The end"
?>
Most scripts do need not to worry about namespaces.
But complex applications may be modularized.
And callbacks should always use namespace current, eg.
<?tht
namespace eval ::foo {
Mod export
proc bar {n} { return bar=[incr n] }
proc callme {cmd} { return callme=[eval $cmd] }
}
proc Work {n} { return [incr n] }
proc Sched {n} {
set x [foo bar $n]
set y [foo callme [list [namespace current]::Work $n]]
echo "Got $x, $y"
}
eval Sched 0
?>
THT includes support for Modlite which allows
ensemble like features in Tcl 8.3.
NOTE: When used with -subinterp 0 the namespace is incrementally assigned as
main_N where N is a unique integer.
© 2008 Peter MacDonald