Posted to tcl by bairui at Tue Sep 08 23:11:59 GMT 2020view raw

  1. #!/usr/bin/env tclsh
  2.  
  3. namespace eval myapp {}
  4.  
  5. proc ::myapp::main {argv0 argv} {
  6. variable reload 0
  7. puts "frame=<[info frame]>"
  8.  
  9. while {([puts -nonewline "? "] eq "") && ([flush stdout] eq "") && [gets stdin line]} {
  10. switch -- $line {
  11. r {set ::myapp::reload 1 ; break}
  12. default {puts $line}
  13. }
  14. }
  15.  
  16. if {$reload} {
  17. # Reload the server script and restart the server.
  18. uplevel #0 [list source [info script]]
  19. if {[info commands tailcall] eq {tailcall}} {
  20. puts "tail calling main"
  21. tailcall ::myapp::main $argv0 $argv
  22. } else {
  23. puts "recursing main"
  24. ::myapp::main $argv0 $argv
  25. }
  26. }
  27. }
  28.  
  29. puts "\nLoading script: ::myapp::reload=<[expr {[info exists ::myapp::reload] ? $::myapp::reload : {}}]>"
  30.  
  31. # If this is the main script...
  32. if {[info exists argv0]
  33. && ([file tail [info script]] eq [file tail $argv0])} {
  34. # If this is not a reload...
  35. if {![info exists ::myapp::reload] || $::myapp::reload} {
  36. ::myapp::main $argv0 $argv
  37. }
  38. }
  39.