summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordkf <donal.k.fellows@manchester.ac.uk>2023-05-31 10:33:01 (GMT)
committerdkf <donal.k.fellows@manchester.ac.uk>2023-05-31 10:33:01 (GMT)
commit38c9c34398bc61793d4794a1c4dadb3a36f23847 (patch)
tree60950a8e49337252996f44d01e2adcfb7292ae94
parentd3acda84955821249bc38d697afe3cc890ac1bf2 (diff)
downloadtcl-38c9c34398bc61793d4794a1c4dadb3a36f23847.zip
tcl-38c9c34398bc61793d4794a1c4dadb3a36f23847.tar.gz
tcl-38c9c34398bc61793d4794a1c4dadb3a36f23847.tar.bz2
Swapped foreachLine arg order, improved docs
-rw-r--r--doc/library.n9
-rw-r--r--library/foreachline.tcl2
-rw-r--r--library/readfile.tcl2
-rw-r--r--library/writefile.tcl5
4 files changed, 11 insertions, 7 deletions
diff --git a/doc/library.n b/doc/library.n
index ce1174c..984dc8a 100644
--- a/doc/library.n
+++ b/doc/library.n
@@ -246,14 +246,17 @@ boundaries before the starting point in the given string. The index
returned refers to the second character of the pair that comprises a
boundary.
.TP
-\fBforeachLine \fIfilename varName body\fR
+\fBforeachLine \fIvarName filename body\fR
.VS "Tcl 8.7, TIP 670"
This reads in the text file named \fIfilename\fR one line at a time
(using system defaults for reading text files). It writes that line to the
variable named by \fIvarName\fR and then executes \fIbody\fR for that line.
The result value of \fIbody\fR is ignored, but \fBerror\fR, \fBreturn\fR,
-\fBbreak\fR and \fBcontinue\fR may be used within it.
-The overall result of \fBforeachLine\fR is the empty string; the file will be
+\fBbreak\fR and \fBcontinue\fR may be used within it to produce an error,
+return from the calling context, stop the loop, or go to the next line
+respectively.
+The overall result of \fBforeachLine\fR is the empty string (assuming no
+errors from I/O or from evaluating the body of the loop); the file will be
closed prior to the procedure returning.
.VE "Tcl 8.7, TIP 670"
.TP
diff --git a/library/foreachline.tcl b/library/foreachline.tcl
index 06ad62a..aacbd5b 100644
--- a/library/foreachline.tcl
+++ b/library/foreachline.tcl
@@ -9,7 +9,7 @@
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
-proc foreachLine {filename varName body} {
+proc foreachLine {varName filename body} {
upvar 1 $varName line
set f [open $filename "r"]
try {
diff --git a/library/readfile.tcl b/library/readfile.tcl
index 350bcd4..c1d5b84 100644
--- a/library/readfile.tcl
+++ b/library/readfile.tcl
@@ -14,7 +14,7 @@ proc readFile {filename {mode text}} {
set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode]
# Read the file
- set f [open $filename [expr {$mode eq "text" ? "r" : "rb"}]]
+ set f [open $filename [dict get {text r binary rb} $mode]]
try {
return [read $f]
} finally {
diff --git a/library/writefile.tcl b/library/writefile.tcl
index ca3bbcc..fbd9138 100644
--- a/library/writefile.tcl
+++ b/library/writefile.tcl
@@ -21,13 +21,14 @@ proc writeFile {args} {
set mode [tcl::prefix match -message "mode" -error $ERR $MODES $mode]
}
default {
+ set COMMAND [lindex [info level 0] 0]
return -code error -errorcode {TCL WRONGARGS} \
- "wrong # args: should be \"[lindex [info level 0] 0] filename ?mode? data\""
+ "wrong # args: should be \"$COMMAND filename ?mode? data\""
}
}
# Write the file
- set f [open $filename [expr {$mode eq "text" ? "w" : "wb"}]]
+ set f [open $filename [dict get {text w binary wb} $mode]]
try {
puts -nonewline $f $data
} finally {