Posted to tcl by stu at Mon Jan 10 13:45:42 GMT 2022view raw

  1. $ cat nsy.tcl
  2.  
  3. namespace eval main {
  4. namespace eval module_a {
  5. proc openModule {args} { return open:$args }
  6. proc closeModule {args} { return close:$args }
  7.  
  8. proc module_a {args} {
  9. namespace path [namespace parent]
  10. set me [namespace tail [lindex [info level 0] 0]]
  11. namespace ensemble create -prefixes no -command $me \
  12. -map [dict create open openModule close closeModule]
  13. rename $me _$me
  14. # Module-specific initialization begin
  15. # put code here
  16. # Module-specific initialization end
  17. tailcall $me {*}$args
  18. }
  19. namespace export module_a
  20. }
  21. namespace import module_a::*
  22. }
  23.  
  24. $ tclsh
  25. % source nsy.tcl
  26. % namespace eval main { list [module_a open a b c] [module_a close 1 2 3] [module_a open x y z] }
  27. {open:a b c} {close:1 2 3} {open:x y z}
  28.  
  29.  
  30. $ tclsh
  31. % source nsy.tcl
  32. % list [main::module_a open a b c] [main::module_a close 1 2 3] [main::module_a open x y z]
  33. {open:a b c} {close:1 2 3} {open:x y z}
  34.  
  35. $ tclsh
  36. % source nsy.tcl
  37. % main::module_a z
  38. unknown subcommand "z": must be close, or open
  39.