foreachline.tcl 608 B

12345678910111213141516171819202122232425
  1. # foreachLine:
  2. # Iterate over the contents of a file, a line at a time.
  3. # The body script is run for each, with variable varName set to the line
  4. # contents.
  5. #
  6. # Copyright © 2023 Donal K Fellows.
  7. #
  8. # See the file "license.terms" for information on usage and redistribution
  9. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10. #
  11. proc foreachLine {varName filename body} {
  12. upvar 1 $varName line
  13. set f [open $filename "r"]
  14. try {
  15. while {[gets $f line] >= 0} {
  16. uplevel 1 $body
  17. }
  18. } on return {msg opt} {
  19. dict incr opt -level
  20. return -options $opt $msg
  21. } finally {
  22. close $f
  23. }
  24. }