Richard Suchenwirth 2006-04-26 - A question on comp.lang.tcl brought me to experiment with code that rewrites itself. The idea is that settings are not saved in a separate resource file, but in the script itself, as setting a global variable with the desired value. So the next time the script is run, the previously saved setting applies. Here's what I did:
proc save {filename varlist} { set f [open $filename] set data [read $f] close $f set lines {} foreach line [split $data \n] { foreach var $varlist { if [regexp "^ *set $var " $line] { set line [list set $var [set ::$var]] } } lappend lines $line } set f [open $filename w] puts $f [join $lines \n] close $f }
#-- Now for a testing demo:
package require Tk
#-- We will test with this variable:
set foo hello label .1 -text foo: entry .2 -textvar foo button .3 -text Save -command [list save [info script] foo] eval pack [winfo children .] -side left
#-- Invaluable little helper for rapid restart:
bind . <Escape> {exec wish $argv0 &; exit}