What is the tcl equivalent of the following?
cat <<EOF > example/file.txt
example
file
contents
EOF
What I have so far:
set outfile [open "example/file.txt" w]
try {
puts $outfile "example
file
contents"
} finally {
close $outfile
}
What I dislike:
The way to deal with the “complexity” is to put the code in a procedure. Then you can ignore the complexity.
proc writefile {filename contents} {
set outfile [open $filename w]
try {
puts $outfile $contents
} finally {
close $outfile
}
}
writefile example/file.txt "example
file
contents"
If you want, you can add in suitable trimming of the contents before writing (Tcl doesn't trim things unless you ask it to).
proc writefile {filename contents} {
set outfile [open $filename w]
try {
puts $outfile [string trim $contents "\n"]
} finally {
close $outfile
}
}
writefile example/file.txt "
example
file
contents
"
If you want more complex trimming (or other processing) rules, putting them in the procedure means that you don't have to clutter up the rest of your code with them. But the scope of possible rules is very wide; you're rapidly getting into totally application-specific stuff.